B
Bob
This is the function I use in a mailsender class in vb2005 using the
system.net.mail namespace
Public Function SendSMTPNetMail(ByVal msgFrom As String, _
ByVal msgTo As String, _
ByVal msgSubject As String, _
ByVal msgBody As String, _
Optional ByRef ExStr As String = "", _
Optional ByVal msgCC As String = "", _
Optional ByVal Attachments As String = "", _
Optional ByVal SMTPServer As String = "localhost") As Boolean
'Attachments is a semiclon separated string of the fully qualified path of
the attachment(s) if there is at least one
Try
Dim EmailMessage As New MailMessage
Dim AddressFrom As New MailAddress(msgFrom) 'there can only be one from
address
EmailMessage.From = addressFrom
EmailMessage.IsBodyHtml = False
EmailMessage.Subject = msgSubject
EmailMessage.Body = msgBody
'There can be multiple to adresses, there must be at least one
Dim ToArray() As String
msgTo = Trim(msgTo)
ToArray = msgTo.Split(";")
For Each EmailTo As String In ToArray
Dim ToAddress As New MailAddress(EmailTo)
EmailMessage.To.Add(ToAddress)
Next
'There could be multiple CC or no CC at all
msgCC = Trim(msgCC)
If msgCC > "" Then
Dim ccArray() As String
ccArray = msgCC.Split(";")
For Each CCaddress As String In ccArray
Dim addressCC As New MailAddress(CCaddress)
EmailMessage.CC.Add(addressCC)
Next
End If
Dim emailClient As New SmtpClient(SMTPServer)
emailClient.UseDefaultCredentials = True
emailClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis
'There can be multiple attachments
Dim AttachArray() As String
If Attachments <> "" Then
Attachments = Trim(Attachments)
If Len(Attachments) > 0 Then
AttachArray = Attachments.Split(";")
' Iterate through the attachment array collection
For Each Attachment As String In AttachArray
Dim attachFile As New Attachment(Attachment)
'Add each item to the collection
EmailMessage.Attachments.Add(attachFile)
Next
End If
End If
EmailMessage.Priority = MailPriority.High
emailClient.Send(EmailMessage)
Return True
Catch ex As FormatException
Dim log As New EventLog(My.Application.Info.ProductName, My.Computer.Name,
"SendSMTPNetMail")
log.WriteEntry(ex.Message, EventLogEntryType.Error, 122)
SendSMTPMailFailsToFile(ex.Message, msgFrom, msgTo)
ExStr = ex.Message
Return False
Exit Function
Catch ex As SmtpException
Dim log As New EventLog(My.Application.Info.ProductName, My.Computer.Name,
"SendSMTPNetMail")
log.WriteEntry(ex.Message, EventLogEntryType.Error, 123)
ExStr = ex.Message
Return False
Exit Function
End Try
End Function
The messages get sent OK, however if I enter two destination addresses (for
instance mine in my domain and then the same address again for testing) I
should get two copies of the same message sent to my email account. I only
get one. The other problem is that when I enter my email address on my
domain and my email address for hotmail in the CC I don't get a CC either at
my own address or my hotmail address. What am I doing wrong?
Any help would be appreciated.
Bob
system.net.mail namespace
Public Function SendSMTPNetMail(ByVal msgFrom As String, _
ByVal msgTo As String, _
ByVal msgSubject As String, _
ByVal msgBody As String, _
Optional ByRef ExStr As String = "", _
Optional ByVal msgCC As String = "", _
Optional ByVal Attachments As String = "", _
Optional ByVal SMTPServer As String = "localhost") As Boolean
'Attachments is a semiclon separated string of the fully qualified path of
the attachment(s) if there is at least one
Try
Dim EmailMessage As New MailMessage
Dim AddressFrom As New MailAddress(msgFrom) 'there can only be one from
address
EmailMessage.From = addressFrom
EmailMessage.IsBodyHtml = False
EmailMessage.Subject = msgSubject
EmailMessage.Body = msgBody
'There can be multiple to adresses, there must be at least one
Dim ToArray() As String
msgTo = Trim(msgTo)
ToArray = msgTo.Split(";")
For Each EmailTo As String In ToArray
Dim ToAddress As New MailAddress(EmailTo)
EmailMessage.To.Add(ToAddress)
Next
'There could be multiple CC or no CC at all
msgCC = Trim(msgCC)
If msgCC > "" Then
Dim ccArray() As String
ccArray = msgCC.Split(";")
For Each CCaddress As String In ccArray
Dim addressCC As New MailAddress(CCaddress)
EmailMessage.CC.Add(addressCC)
Next
End If
Dim emailClient As New SmtpClient(SMTPServer)
emailClient.UseDefaultCredentials = True
emailClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis
'There can be multiple attachments
Dim AttachArray() As String
If Attachments <> "" Then
Attachments = Trim(Attachments)
If Len(Attachments) > 0 Then
AttachArray = Attachments.Split(";")
' Iterate through the attachment array collection
For Each Attachment As String In AttachArray
Dim attachFile As New Attachment(Attachment)
'Add each item to the collection
EmailMessage.Attachments.Add(attachFile)
Next
End If
End If
EmailMessage.Priority = MailPriority.High
emailClient.Send(EmailMessage)
Return True
Catch ex As FormatException
Dim log As New EventLog(My.Application.Info.ProductName, My.Computer.Name,
"SendSMTPNetMail")
log.WriteEntry(ex.Message, EventLogEntryType.Error, 122)
SendSMTPMailFailsToFile(ex.Message, msgFrom, msgTo)
ExStr = ex.Message
Return False
Exit Function
Catch ex As SmtpException
Dim log As New EventLog(My.Application.Info.ProductName, My.Computer.Name,
"SendSMTPNetMail")
log.WriteEntry(ex.Message, EventLogEntryType.Error, 123)
ExStr = ex.Message
Return False
Exit Function
End Try
End Function
The messages get sent OK, however if I enter two destination addresses (for
instance mine in my domain and then the same address again for testing) I
should get two copies of the same message sent to my email account. I only
get one. The other problem is that when I enter my email address on my
domain and my email address for hotmail in the CC I don't get a CC either at
my own address or my hotmail address. What am I doing wrong?
Any help would be appreciated.
Bob