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
Software/Hardware used:
ASKED:
October 22, 2009 2:54 PM
UPDATED:
October 22, 2009 7:05 PM
You will need to provide the exact error message, and the line of code where it is being raised.
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
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 (\).