How can I create or send Smtp multiple attachments in email using VB.NET 2008
15 pts.
0
Q:
How can I create or send Smtp multiple attachments in email using 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

ASKED: Oct 22 2009  2:54 PM GMT
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
0
15 pts.
0
A:
 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0
  • AddThis Social Bookmark Button
Last Answered: Oct 22 2009  2:54 PM GMT by Mtang2009   15 pts.
0
0
Discuss This Answer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _



_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Carlosdl   29710 pts.  |   Oct 22 2009  4:21PM GMT

You will need to provide the exact error message, and the line of code where it is being raised.

 

Mtang2009   15 pts.  |   Oct 22 2009  5:28PM GMT

Hello Carlos,

Please bare with me, I’m new to this websit ok! Here are the error

Readfile = <a href="http://My.Com" title="http://My. " target="_blank">My.Com</a>puter.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

 

Carlosdl   29710 pts.  |   Oct 22 2009  7:05PM GMT

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 (\).

 
0