Click here to Skip to main content
15,889,216 members
Articles / Programming Languages / Visual Basic

VB.NET 2005 SMTP Email Message

Rate me:
Please Sign up or sign in to vote.
3.50/5 (25 votes)
11 Jul 2006CPOL 196.8K   2.4K   75   30
Send an SMTP mail message with multiple recipients and multiple file attachments.

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:

VB
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)
        'This procedure takes string array parameters for multiple recipients and files
        Try
            For Each item As String In strTo
                'For each to address create a mail message
                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

                'attach each file attachment
                For Each strfile As String In fileList
                    If Not strfile = "" Then
                        Dim MsgAttach As New Attachment(strfile)
                        MailMsg.Attachments.Add(MsgAttach)
                    End If
                Next

                'Smtpclient to send the mail message
                Dim SmtpMail As New SmtpClient
                SmtpMail.Host = "10.10.10.10"
                SmtpMail.Send(MailMsg)
            Next
            'Message Successful
        Catch ex As Exception
            'Message Error
        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)
        'This procedure overrides the first procedure and accepts a single
        'string for the recipient and file attachement 
        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

            'Smtpclient to send the mail message
            Dim SmtpMail As New SmtpClient
            SmtpMail.Host = "10.10.10.10"
            SmtpMail.Send(MailMsg)
        Catch ex As Exception
            'Message Error
        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.

XML
<system.net>    
<mailSettings>
<!-- these settings define the mail server settings
    from: the user name from which the email is sent - this is the
    application that is sending the message
    host: the name of your mail server
    userName: the name the application will use to log into the mail server
    password: the password for the above user name      -->
  <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:

VB
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" 'The body encoding is set to HTML 

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
.Net Developer

Comments and Discussions

 
GeneralMy vote of 1 Pin
RAGHVE30-May-11 21:54
RAGHVE30-May-11 21:54 
Generalworking in window form Pin
Richard Herdi26-Jan-11 15:16
Richard Herdi26-Jan-11 15:16 
GeneralHaving Poblem Sending Email from ASP.Net Pin
lord mark N28-Jan-10 2:48
lord mark N28-Jan-10 2:48 
QuestionEmail Download Pin
vin.owen14-Dec-09 22:56
vin.owen14-Dec-09 22:56 
QuestionWhy do you need to overload this method? Pin
Peter Stanford26-Oct-09 18:31
Peter Stanford26-Oct-09 18:31 
GeneralError Pin
rikitanagar25-Aug-09 23:52
rikitanagar25-Aug-09 23:52 
GeneralEmails are not being sent Pin
rgriggs31-Dec-08 6:59
rgriggs31-Dec-08 6:59 
GeneralRe: Emails are not being sent Pin
lord mark N26-Jan-10 5:27
lord mark N26-Jan-10 5:27 
GeneralThe code executes, but no mail is received. Pin
Jerodr4-Jun-08 8:31
Jerodr4-Jun-08 8:31 
GeneralSmtpMail.Host = "10.10.10.10" Pin
baruch_a28-Apr-08 2:15
baruch_a28-Apr-08 2:15 
GeneralRe: SmtpMail.Host = "10.10.10.10" Pin
mamboh10-Jan-09 21:08
mamboh10-Jan-09 21:08 
GeneralEmail ends up in Junk Box Pin
J Sullivan8-Oct-07 11:39
J Sullivan8-Oct-07 11:39 
Hi,

Do you know how to stop email from ending up in Junk Box?

Currently have problem with VET anti-virus and Vista.
GeneralInvalid URI: The format of the URI could not be determined. Pin
Aldwin Galapon28-Sep-07 22:47
Aldwin Galapon28-Sep-07 22:47 
QuestionNot sending in timely manner Pin
chetton200021-May-07 8:35
chetton200021-May-07 8:35 
QuestionUnable to send mail Pin
Micha Brans26-Mar-07 1:15
Micha Brans26-Mar-07 1:15 
Generalerror Pin
shakti538510-Jan-07 20:22
shakti538510-Jan-07 20:22 
QuestionEncoding.Default ?? Pin
HART215-Jul-06 0:18
HART215-Jul-06 0:18 
AnswerRe: Encoding.Default ?? Pin
Edacio5-Jul-06 3:53
Edacio5-Jul-06 3:53 
GeneralRe: Encoding.Default ?? Pin
HART215-Jul-06 4:30
HART215-Jul-06 4:30 
GeneralRe: Encoding.Default ?? Pin
Edacio6-Jul-06 4:10
Edacio6-Jul-06 4:10 
GeneralRe: Encoding.Default ?? Pin
HART216-Jul-06 8:07
HART216-Jul-06 8:07 
AnswerRe: Encoding.Default ?? Pin
eddybt@cantv.net20-Jul-07 8:00
eddybt@cantv.net20-Jul-07 8:00 
GeneralRe: Encoding.Default ?? Pin
mike_hoot26-Nov-07 2:33
mike_hoot26-Nov-07 2:33 
GeneralAuthorization Pin
pyramidak3-Jul-06 2:35
pyramidak3-Jul-06 2:35 
GeneralRe: Authorization Pin
Edacio3-Jul-06 3:47
Edacio3-Jul-06 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.