How can I create or send Smtp multiple attachments in email using VB.NET 2008

15 pts.
VB.NET 2008
I am working on vb dot net  code  which is used to send email from the desktop application, it is not working CORRECTLLY.  I need your expertee help Please !!!!! The main problem arises when I tried to attach multiple files or i tried to attach a file in the array and for loop.  Below is the codes I have used to implement. Imports

Imports System.Net







Public lastfile As Short


Public Readfile As StreamReader  

Public Filename(10000) As String


Public Cnter, Counts As Integer


Public inpath As String = "C:Mailer"


Public FromAddress As String = ""


Public ToAddress As String = ""


Public CCAddress As String = ""


Sub main()  

Dim strdate As String = ""


Dim subject As String = "SOLSGB Send daily data and Reports"


Dim body As String = "Please see attached files..."


Dim Filedata As String = ""


Dim SendLog As String = ""


Dim LogData As String = ""


Dim linedata As String


Dim TypeOfLine As String


Dim tmpstr As String




Dim fileReader As StreamReader fileReader =


Do Until fileReader.EndOfStream linedata = fileReader.ReadLine()

TypeOfLine = Trim(Mid(linedata, 1, 13))

tmpstr = Trim(Mid(linedata, 16, 35))


Select Case TypeOfLine  

Case Is = "From Address"

FromAddress = tmpstr


Case Is = "To Address"

ToAddress = tmpstr


Case Is = "CC Address"

CCAddress = tmpstr


Case Else


End Select





Catch ex As Exception  

My.Computer.FileSystem.WriteAllText("C:mailertxtUNKSend.log", "Config File, C:mailertxtmail.txt, or Path, not available." & vbCrLf, True)  

GoTo Finish  

End Try


Dim Attachfiles As String = ""



Dim MailToSend As Boolean = False


Dim CC As String = ""




Dim MailStatus As New SmtpStatusCode  

' MailMessage is used to represent the e-mail being sent


Using message As New MailMessage(FromAddress, ToAddress, subject, body) message.CC.Add(CC)


If UCase(Mid(Filename(Cnter), InStr(Filename(Cnter), ".") + 1, 3)) = "RTF" Then

FileOpen(1, inpath & Filename(Cnter), OpenMode.Append)


End If


For Cnter = 0 To UBound(Filename) Counts = Filename(Cnter) - 1


If UCase(Mid(Filename(Cnter), InStr(Filename(Cnter), "."))) = "RTF" Then


"C:Mailer" & Filename(Cnter), OpenMode.Append)  

End If

Readfile =

My.Computer.FileSystem.OpenTextFileReader("C:Mailer" & (Filename(Cnter)))  

Do Until Readfile.EndOfStream  

If My.Computer.FileSystem.FileExists(Filename(Cnter)) Then

Attachfiles = Attachfiles(

"C:Mailer" & (Filename(Cnter))) message.Attachments.Add(

New Attachment(Attachfiles))  



GoTo Finish  

End If




Next Cnter




Dim mailServerName As String = ""


'Dim mailServerName As String = "localhost"


' SmtpClient is used to send the e-mail


Dim mailClient As New SmtpClient(mailServerName)  

mailClient.UseDefaultCredentials = True


Console.WriteLine("Sending File...")  

' Send delivers the message to the mail server



End Using


Console.WriteLine("Message Sent OK", MailStatus)  

Catch ex As FormatException Print(1,

"Format Exception Fail" & ex.Message & " " & Now & vbCrLf)


Catch ex As SmtpException  

Print(1, "SMTP Exception Fail" & ex.Message & " " & Now & vbCrLf)


End Try



End Sub


End Module

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question: 3  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • carlosdl
    You will need to provide the exact error message, and the line of code where it is being raised.
    80,565 pointsBadges:
  • Mtang2009
    Hello Carlos, Please bare with me, I'm new to this websit ok! Here are the error Readfile = My.Computer.FileSystem.OpenTextFileReader(inpath & (Filename(Cnter))) ArgumentException Occured (This is an error message ) The given file path ends with a directory separator character. Parameter name: file Please let me know, if you need more question Thanks Minh
    15 pointsBadges:
  • carlosdl
    The problem seems to be the argument you are sending to the OpenTextFileReader method. I would recommend displaying a message box with the contents of "inpath & (Filename(Cnter))" before calling OpenTextFileReader. An ArgumentException is raised when the file name ends with a backslash (\).
    80,565 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: