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

15 pts.
Tags:
SMTP
VB.NET
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

 

System.net.mail

Imports System.Net

Imports

 

System.IO

Module

 

Mailer  



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

 

Try

 

Dim fileReader As StreamReader fileReader =



My.Computer.FileSystem.OpenTextFileReader("C:mailertxtmail.txt")  



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

 

Loop

fileReader.Close()

 

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 = "Solog1@charterinternet.com"

 

Try

 

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

FileOpen(1,

"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))  



Else

 

GoTo Finish  



End If

 

Loop

 

Next Cnter

Readfile.Close()

FileClose(1)

 



Dim mailServerName As String = "smtp.charterinternet.com"

 

'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

mailClient.Send(message)

 

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

 

'Finish:  



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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    You will need to provide the exact error message, and the line of code where it is being raised.
    69,420 pointsBadges:
    report
  • 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:
    report
  • 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 (\).
    69,420 pointsBadges:
    report

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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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

Following