Introduction
I needed to send an SMTP email to multiple recipients with multiple attachments using VB.NET 2005. Sending an SMTP email in VB.NET 2005 has changed a little since VB.NET 2003. I have updated the class so that the SendEmailMessage
procedure is overloaded to accept either multiple recipients and multiple attachments or a single recipient and a single attachment. This makes it a little easier so that you do not need to instantiate two string arrays if you are only sending an email to one recipient.
Create a class file with this code:
Imports Microsoft.VisualBasic
Imports System.net.Mail
Public Class SendEmail
Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo() _
As String, ByVal strSubject _
As String, ByVal strMessage _
As String, ByVal fileList() As String)
Try
For Each item As String In strTo
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), _
New MailAddress(item))
MailMsg.BodyEncoding = Encoding.Default
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True
For Each strfile As String In fileList
If Not strfile = "" Then
Dim MsgAttach As New Attachment(strfile)
MailMsg.Attachments.Add(MsgAttach)
End If
Next
Dim SmtpMail As New SmtpClient
SmtpMail.Host = "10.10.10.10"
SmtpMail.Send(MailMsg)
Next
Catch ex As Exception
End Try
End Sub
Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo _
As String, ByVal strSubject _
As String, ByVal strMessage _
As String, ByVal file As String)
Try
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), _
New MailAddress(strTo))
MailMsg.BodyEncoding = Encoding.Default
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True
If Not file = "" Then
Dim MsgAttach As New Attachment(file)
MailMsg.Attachments.Add(MsgAttach)
End If
Dim SmtpMail As New SmtpClient
SmtpMail.Host = "10.10.10.10"
SmtpMail.Send(MailMsg)
Catch ex As Exception
End Try
End Sub
End Class
Instead of setting the SMTP host settings inside the class, you can configure it inside the Web.Config file.
<system.net>
<mailSettings>
<smtp from="admin@your-domain.com">
<network host="your-mail-server-name"
userName="your-user-name"
password="your-password" />
</smtp>
</mailSettings>
</system.net>
Use this code in your form code or in an ASP.NET code-behind:
Dim SendEmail As New SendEmail
Dim SendTo(2) As String
Dim FileAttach(2) As String
Dim strSubject As String
Dim strMessage As String
SendTo(0) = "someone@email.com"
SendTo(1) = "someoneelse@email.com"
FileAttach(0) = "c:\text.txt"
FileAttach(1) = "c:\Othertext.txt"
strSubject = "Email Subject"
strMessage = "Email Messge Text"
SendEmail.SendEmailMessage("fromsomeone@email.com", SendTo, _
strSubject, strMessage, FileAttach)
Since SendEmailMessage
is overloaded, you can send an email to a single recipient with a single attachment.
Dim SendEmail As New SendEmail
Dim SendTo As String
Dim FileAttach As String
Dim strSubject As String
Dim strMessage As String
SendTo = "someone@email.com"
FileAttach(0) = "c:\text.txt"
strSubject = "Email Subject"
strMessage = "Email Messge Text" 'The body encoding is set to HTML
SendEmail.SendEmailMessage("fromsomeone@email.com", SendTo, _
strSubject, strMessage, FileAttach)
History
- 07-11-2006: Article and source updated.