VB.net

Hi Experts,

I want to mail one or more files which 're dropped on my vb.net application.

Imports System.Net.Mail
...
 .Attachments.Add(New Attachment(txt))
...
If txt contains long file name with space i.e.
C:\Users\user1\Desktop\Sicherungsscripte Highlights

following excetion occurs:
System.ArgumentException: ".. illegal char in path .." (hope my translation is correct)

With Files without Space in between, application works fine.

Whats my bug?

THX
LVL 4
reredokIT ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Om PrakashCommented:
Specify the attachment name
attachment.Name = "filename"
0
Om PrakashCommented:
Try
Dim myEmail As New System.Net.Mail.MailMessage()
        myEmail.From = New MailAddress("someuser@test.com")
        myEmail.To.Add("someuser@user.com")
        myEmail.Subject = "test mail with attachment"
        myEmail.Body = "this is test mail with attachment"
        Dim attachement As New System.Net.Mail.Attachment("g:\test file.txt")
        attachement.Name = "filenameinmail.txt"
        myEmail.Attachments.Add(attachement)
        Dim EmailServer As New System.Net.Mail.SmtpClient("http://localhost")
        EmailServer.Send(myEmail)
        MessageBox.Show("Done !!")

Open in new window

0
13598Commented:
It should work with a space.
What type do your have your variable txt declare as?
Could it be it is truncating the name?
Did you try it with the actual name (hardcoding it to test)  and it works?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

reredokIT ConsultantAuthor Commented:
Dim txt As String = ""

Yes hardcoding filename works:

.Attachments.Add(New Attachment("C:\Users\user1\Desktop\Sicherungsscripte Highlights")

No in the direct debug windows the varible txt seem to bee ok no  truncating
0
13598Commented:
It could be because there is no filetype extension. Could you add the extension to txt as in txt = txt & ".txt" or whatever extension so it knows where the filename ends. I believe that is the problem without extension it may consider the space the end of the filename and when there is more after it conisders it invalid.
0
reredokIT ConsultantAuthor Commented:
No :-(

Calling with txt = "C:\Users\user1\Desktop\Sicherungsscripte Highlights.org" don't works too.

crazy! If I drag n drop to a listbox (same filename)
...
                listboxstr(n) = ListBox1.Items(n - 1).ToString
                .Attachments.Add(New Attachment(listboxstr(n)))
...
it works.
despairingly I add my Command$ to the listbox. Same error. It must be txt !

0
13598Commented:
How do you assign it the value? Could you post the statement where you do that?
0
reredokIT ConsultantAuthor Commented:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '
        Dim cmds As String = ""
        Dim txt As String = ""
        Dim pos As Integer = 0

        cmds = Command$()

        If Len(cmds) = 0 Then Exit Sub

        ListBox1.Items.Clear()

        Do While Len(cmds) > 0
            pos = InStr(cmds, " ")
            If pos = 0 Then
                txt = txt & cmds
                cmds = ""
            Else
                txt = txt & Leftfct(cmds, pos - 1) & vbCrLf
                cmds = Mid$(cmds, pos + 1)
           End If
        Loop
        eMailSendViaDragNDrop()
--------------------------------------------------------
--------------------------------------------------------
    Public Function Leftfct(ByVal sText As String, _
  ByVal nLen As Integer) As String

        If nLen > sText.Length Then nLen = sText.Length
        Return (sText.Substring(0, nLen))
    End Function

Open in new window

0
reredokIT ConsultantAuthor Commented:
pos = InStr(cmds, ";") was first approach to spilt multiple files with ; delemiter.
pos = InStr(cmds, " "). Trim(txt) (tried)
0
13598Commented:
Could you add this right before your eMailSendViaDragNDrop()
messagebox.show(txt)
and post it.

Also could you change your statement
Attachments.Add(New Attachment(txt))
to:
Attachments.Add(New Attachment(txt.tostring.trim))


0
13598Commented:
"pos = InStr(cmds, ";") was first approach to spilt multiple files with ; delemiter"
why don't you use Split?
0
13598Commented:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim cmds As String = ""

        Dim txt As String = ""
        Dim pos As Integer = 0

        cmds = Command$()

        If Len(cmds) = 0 Then Exit Sub

        ListBox1.Items.Clear()

   Dim myfilelist As String() = cmds.Split(New Char() {" "c})  

            For Each txt In myfilelist
           messagebox.show( txt )
        Next
       
        eMailSendViaDragNDrop()
0
reredokIT ConsultantAuthor Commented:


Input: C:\Users\koderer.APS\Desktop\Sicherungsscripte Highlights.org
watch:             txt      "C:\Users\koderer.APS\Desktop\Sicherungsscripte"      String
0
13598Commented:
Change it to parse by a period instead of a space. The character you want to parse by is the period?
You need txt to be "C:\Users\user1\Desktop\Sicherungsscripte Highlights" ?
0
13598Commented:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim cmds As String = ""

        Dim txt As String = ""
        Dim pos As Integer = 0

        cmds = Command$()

        If Len(cmds) = 0 Then Exit Sub

        ListBox1.Items.Clear()

   Dim myfilelist As String() = cmds.Split(New Char() {"."c})  

            For Each txt In myfilelist
           messagebox.show( txt )
        Next
0
13598Commented:
I guess I don't understand why if the input is C:\Users\koderer.APS\Desktop\Sicherungsscripte Highlights.org you need to parse it?
Isn't that the file you want attached?
0
reredokIT ConsultantAuthor Commented:
cause I want to drag n drop multiple Files. So I must parse the Command$().
0
13598Commented:
WPrivate Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim cmds As String = ""

        Dim txt As String = ""
        Dim pos As Integer = 0

        cmds = Command$()

        If Len(cmds) = 0 Then Exit Sub

        ListBox1.Items.Clear()

   Dim myfilelist As String() = cmds.Split(New Char() {","c})  

            For Each txt In myfilelist
           eMailSendViaDragNDrop()
        Next
hat are they separated by? commas?  Then use comma as a separator.
0
13598Commented:


What are they separated by? commas?  Then use comma as a separator.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim cmds As String = ""

        Dim txt As String = ""
        Dim pos As Integer = 0

        cmds = Command$()

        If Len(cmds) = 0 Then Exit Sub

        ListBox1.Items.Clear()

   Dim myfilelist As String() = cmds.Split(New Char() {","c})  

            For Each txt In myfilelist
           eMailSendViaDragNDrop()
        Next
0
reredokIT ConsultantAuthor Commented:
let's take a step back:
my vb.net exe is on my windows desktop and not startet. I drag 'n' drop 2 Files and my
cmds = command$() is
 "C:\Users\user1\Desktop\File1.txt C:\Users\user1\Desktop\File2.txt"
At once there is no delimiter nessesary. The delimter between filename ist a Space " "
 Now I must assign this String to System.Net.Mail.Attachment.Add (...)
So I used (copy a code from Internet :-) ) i.e. ";" and split in a string array. This works fine if absolute pathname has no Space.
0
13598Commented:
Parsing by one space won't cut it when you allow spaces in the filenames.
So the issue is that when it parses that file it truncates it?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
reredokIT ConsultantAuthor Commented:
Then my beginning was wrong.
Drag n Drop with Command$ can't separate Filenames. sadly windows allows spaces in filenames.
0
13598Commented:
A workaround could be to parse by period and concatenate 1 and 2nd elements to get filenames.
One way of doing it could be:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim cmds As String = ""

        Dim txt As String = ""
        Dim pos As Integer = 0

        cmds = Command$()

        If Len(cmds) = 0 Then Exit Sub

        ListBox1.Items.Clear()

   Dim myfilelist As String() = cmds.Split(New Char() {"."c})  
dim iCounter as integer = 0
dim sBuildFilename as string = ""
            For Each sBuildFilename In myfilelist
if iCounter mod 2 = 0 then
txt = sBuildFilename
else
txt = txt & sBuildFilename
eMailSendViaDragNDrop()
                 Next
0
13598Commented:
Missed increasing the counter. Here it goes:
A workaround could be to parse by period and concatenate 1 and 2nd elements to get filenames.
One way of doing it could be:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim cmds As String = ""

        Dim txt As String = ""
        Dim pos As Integer = 0

        cmds = Command$()

        If Len(cmds) = 0 Then Exit Sub

        ListBox1.Items.Clear()

   Dim myfilelist As String() = cmds.Split(New Char() {"."c})  
dim iCounter as integer = 0
dim sBuildFilename as string = ""
            For Each sBuildFilename In myfilelist
if iCounter mod 2 = 0 then
txt = sBuildFilename
else
txt = txt & sBuildFilename
eMailSendViaDragNDrop()
   end if
iCounter = iCounter  + 1
              Next
0
reredokIT ConsultantAuthor Commented:
nice thanks but don't work for me:
'-------------------------------------------------------V----------------Space
Command$="C:\Users\user1\Desktop\toDos.txt C:\Users\user1\Desktop\1.txt"

 myfilelist(1) ="C:\Users\user1"
myfilelist(2) ="Desktop\toDos.txt"" and so on
0
13598Commented:
This should work:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim cmds As String = ""

        Dim txt As String = ""
        Dim pos As Integer = 0

        cmds = Command$()

        If Len(cmds) = 0 Then Exit Sub

        ListBox1.Items.Clear()

        Dim sBuildFilename As String = ""
        Dim bolMissingExt As Boolean = False
        Dim myfilelist As String() = cmds.Split(New Char() {" "c})
        For Each txt In myfilelist
            If bolMissingExt Then
                sBuildFilename = sBuildFilename & " " & txt
            Else
                sBuildFilename = txt
            End If
            If Not txt.Contains(".") Then
                bolMissingExt = True
            Else
                bolMissingExt = False
            End If
if not bolMissingExt then
eMailSendViaDragNDrop
end if
        Next
0
13598Commented:
Actually since eMailSendViaDragNDrop uses txt either change it to use sBuildFileName or reverse the for loop so the end result ends up in the variable txt instead.
 
0
reredokIT ConsultantAuthor Commented:
Jesus ... I'm blind

Controll$():
It's not SPACE. It's Chr(13) & Chr(10)

I located with:
        ' ---- debug
        ReDim temp1(Len(cmds))
        ReDim temp1_ASC(Len(cmds))
        For pos = 1 To Len(cmds)
            temp1(pos) = Mid(cmds, pos, 1)
            temp1_ASC(pos) = Asc(temp1(pos))
        Next
        ' ---- debug

I modifyed your code to:
        Dim myfilelist As String() = cmds.Split(New Char() {Chr(13) & Chr(10)})

and it work!

Great! Thanks a lot. Great mission ......


0
reredokIT ConsultantAuthor Commented:
chr(13) & chr(10) only if optional Paramters in project setting :-(
0
reredokIT ConsultantAuthor Commented:
I found that long filename were inside "". So command$ parse like
C:\dir1\file1.txt "C:\dir1\My File.org" C:\dir2\file2.txt
-----------------I------------------------I-----------------
--------------Space "--------------- " Space----------
So what I need is an commandline parser. Many googled but much too complex for my small-sized vb.net project.
0
13598Commented:
Did you try this? It works for me.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cmds As String = ""

        Dim txt As String = ""
        Dim pos As Integer = 0

        ''  cmds = Command$()
        cmds = "C:\dir1\file1.txt C:\dir1\My File.org C:\dir2\file 2.txt C:\dir2\file2.txt"

        If Len(cmds) = 0 Then Exit Sub

           ListBox1.Items.Clear()

        Dim sBuildFilename As String = ""
        Dim bolMissingExt As Boolean = False
        Dim myfilelist As String() = cmds.Split(New Char() {" "c})
        For Each sBuildFilename In myfilelist
            If bolMissingExt Then
                txt = txt & " " & sBuildFilename
            Else
                txt = sBuildFilename
            End If
            If Not txt.Contains(".") Then
                bolMissingExt = True
            Else
                bolMissingExt = False
            End If
            If Not bolMissingExt Then
                    eMailSendViaDragNDrop()
            End If
        Next
    End Sub
0
reredokIT ConsultantAuthor Commented:
thx for your effort but the Problem is:

"C:\dir1\file1.txt "C:\dir1\My File.org" C:\dir2\file2.txt"

The best way is:
1. eliminate 'correct' SPACE to i.e. ";",
2. string.split
3. eliminate "wrong" "


http://evolvedcode.net/content/code_vbcmdline/

[ ...

Description
If you have ever attempted to make a VB program accept anything beyond the most basic information from the command line via the Command function then you will be acutely aware that the entire system is less than brilliant. There are really two problems - firstly that the Command function returns a simple string and not an array of strings, secondly that the Split function is not smart enough to understand how delimited separators work. The following addresses those issues.
...
Hopefully this should help solve some of those problems - this is a Visual Basic module capable of parsing a command line and turning it into an array of strings. Since it correctly parses delimiters this makes it ideal for applications which involve handling of multiple long filenames, or for that matter any type of information which requires VB to accept delimited input via the command line.
...]
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.