ARTICLE

Send Email Messages and Attachments Using Visual Basic 2008

Posted by Scott Lysle Articles | Visual Basic 2010 August 27, 2008
This article describes an approach to sending email messages with or without attachments. The code required to send the message is contained within a reusable class. In addition to sending messages and messages with attachments, the class also validates the recipient email addresses using regular expression validation.
Download Files:
 
Reader Level:

Introduction

This article describes an approach to sending email messages with or without attachments.  The code required to send the message is contained within a reusable class.  In addition to sending messages and messages with attachments, the class also validates the recipient email addresses using regular expression validation.

 

Figure 1:  Test Application Main Form

 

 

SMTP Configuration

Setting up IIS

Your local IIS instance has to be properly configured in order to successfully send an email message through its SMTP mail server.  Even if you install IIS, the SMTP mail server installation is optional and must be explicitly added to the installation.  If you are not sure whether or not the SMTP mail server is installed, open up the IIS control panel and check for the installation; if it is installed you will see a reference to the Default SMTP Virtual Server in the tree view (Figure 2):

 

Figure 2:  Default SMTP Virtual Server Installed

 

If the server is not installed, you will need to use the “Add and Remove Windows Components” function in the “Add and Remove Programs” control panel to add the SMTP server to your IIS installation.  If you need to do this additional installation, once you’ve opened the “Add and Remove Windows Components”, click on “Internet Information Services (IIS)” to highlight it and then click on the “Details” button (Figure 3).  This will open an IIS dialog; examine this dialog to locate “SMTP Service” and click on it to place a check mark on the box (Figure 4).  Once this item has been checked, click on the “OK” button to install the SMTP server.

 

Figure 3:  Windows Components Wizard

 

Figure 4:  Adding the SMTP Service to IIS

 

Once the default SMTP server has been installed or verified; you can now configure it to send email.  In order to configure the SMTP server, open the IIS control panel, locate the default SMTP server icon in the treeview, and select and right click the icon.  Once the context menu has been displayed, locate “Properties” and click on it to open the properties menu.

Once the Default SMTP Virtual Server Properties dialog is displayed click on the “Access” tab (Figure 5):

Figure 5:  SMTP Property Dialog

 

Select the “Authentication” button to display the authentication options (Figure 7):

Figure 6:  Authentication Options

 

Make sure that the Anonymous access option is checked and that all other options are unchecked; in some instances you may wish to use the other options but in most cases involving a public website this is the option you’d want.  Once you have verified this setting, click “OK” to close this dialog.

Back to the “Access” tab, locate and click on the “Relay” button to display the relay options.  Note that the radio button for “Only the list below” is selected, and that the local host IP address has been added to the list of computers permitted to relay through the SMTP server.  Naturally this is OK for a development machine but in deployment, you would use the actual IP address of the web server.  If no IP addresses are shown in the list, click on the “Add” button and add the IP address.  Once finished, click on the “OK” button to accept the changes and to dismiss the dialog.  (Figure 7)

Figure 7:  Relay Restrictions Dialog

 

Next, select the “Delivery” tab from the SMTP Server properties dialog (Figure 8):

Figure 8:  Delivery Options Dialog

 

From this dialog, select the “Advanced” button to reveal the advanced options dialog (Figure 9):

Figure 9:  Advanced Delivery Dialog

 

From this dialog there are two points to make; first, the “Fully qualified domain name” property should be pre-populated; you may click on the “Check DNS” button to validate the setting.  The next option is probably the most critical item for the whole shooting match; the Smart Host property has to be set to point to a valid SMTP mail server that will permit you to relay mail.  For most cases, you will key in the name of your internet provider’s default SMTP mail server; the address is likely to be in the format of “mail.something.com” where “something” is the internet provider’s domain name.  There are two easy ways to get this, one is, if you are using Outlook, open up Outlook and pull up the information on your email account; the mail server will be listed there.  The second option is to guess and you can qualify your guess by pinging the server. 

If your internet provider’s name is “foxtrox”, try pinging mail.foxtrot.com; if you get a response there, that is probably the one to use.  If that does not work, contact your administrator and ask them for the information on the SMTP mail server; don’t try to plug in an Exchange server, there will likely be an SMTP mail server out there even if your company is using an Exchange server.  The only other hurdle is to make sure that the SMTP mail server will allow you to relay and again, you may need to talk to the administrator if the server bounces your mail.  Once these settings are made, click on the “OK” button to save the settings and close the dialog. 

The last thing to do is to check the security tab to make sure the accounts are properly configured; once done your security settings should look something like this (Figure 10):

 

Figure 10:  Security Settings for the SMTP Server

 

Once everything is setup, click on the “OK” button to save the settings and to close the Default SMTP Virtual Server Properties dialog.

 

Getting Started:

In order to get started, unzip the included project and open the solution in the Visual Studio 2008 environment.   In the solution explorer, you should note these files (Figure 11):

 

Figure 11:  Solution Explorer

 

The solution contains two projects; the EmailHandler project is a class library; it contains a class entitled, “Emailer.vb” which contains all of the code necessary to send email messages with and without attachments, and to validate email addresses.  The second project is entitled, “EmailTestApp”; this is a win forms application which provides an interface that may be used to construct and send email messages using the Emailer class functions.

 

Code:  Emailer.vb

The Emailer class is used to send email messages with or without attachments, and it contains the code used to validate the format of the email addresses used to define the email sender and recipient.

The class begins with the default and added imports; note the addition of System.Net.Mail, System.Net.Mime, and System.Text.RegularExpressions. 

Imports System.Collections

Imports System.Net.Mail

Imports System.Net.Mime

Imports System.Text.RegularExpressions

  

 

The next section contains the class declaration. 

 

 

Public Class Emailer

 

 

The first method contained in this class is used to send an email message with or without an attachment and with or without a list of blind copy recipients; the attachment and blind copy lists are both optional arguments to this method. 

The method collects the recipient email address, the sender’s email address, the email subject line, and the message body as arguments along with the optional lists of blind copy recipients and attachments. 

Within the code, the recipient’s email address is validated, a new mail message is constructed using the passed in arguments, an instance of an SMTP client is created using the mail server (stored in the properties), a Boolean is set to enable the requests to include the default credentials, and then the message is sent.  If the action succeeds, the method will return a string indicating that the submittal was successful, else, the exception message will be returned to the caller.

 

    ''' <summary>

    ''' Transmit an email message

    ''' </summary>

    ''' <param name="sendTo">Recipient Email Address</param>

    ''' <param name="sendFrom">Sender Email Address</param>

    ''' <param name="sendSubject">Subject Line Describing Message</param>

    ''' <param name="sendMessage">The Email Message Body</param>

    ''' <param name="attachments">A string array pointing to the location of 

        each attachment</param>

    ''' <returns>Status Message as String</returns>

    Public Shared Function SendEmailMessage(ByVal sendTo As String, _

                             ByVal sendFrom As String, _

                             ByVal sendSubject As String, _

                             ByVal sendMessage As String, _

                             Optional ByVal toBcc As ArrayList = Nothing, _

                             Optional ByVal attachments As ArrayList =

                             Nothing) As String

 

        Try

 

            ' validate recipient email address

            Dim bTest As Boolean = ValidateEmailAddress(sendTo)

 

            If (bTest = False) Then

                Return "Invalid recipient email address: " + sendTo

            End If

 

            ' validate sender email address

            bTest = ValidateEmailAddress(sendFrom)

 

            If (bTest = False) Then

                Return "Invalid sender email address: " + sendTo

            End If

 

            ' Create the basic message

            Dim message As New MailMessage( _

               sendFrom, _

               sendTo, _

               sendSubject, _

               sendMessage)

 

            ' add blind copy recipients

            If (Not toBcc Is Nothing) Then

 

                If (toBcc.Count > 0) Then

                    Dim sBcc As String

 

                    For Each sBcc In toBcc

                        If (sBcc <> "") Then

                            bTest = ValidateEmailAddress(sBcc)

 

                            If (bTest = False) Then

                                Return "Invalid Blind Copy email address: " +

                                sBcc

                            Else

                                message.Bcc.Add(sBcc)

                            End If

                        End If

                    Next

                End If

            End If

 

            ' The attachments arraylist should point to a file location where

            ' the attachment resides - add the attachments to the message

            If (Not attachments Is Nothing) Then

 

                Dim attach As String

                For Each attach In attachments

                    Dim attached As New Attachment(attach,

                    MediaTypeNames.Application.Octet)

                    message.Attachments.Add(attached)

                Next

 

            End If

 

            ' create smtp client at mail server location

            Dim client As New SmtpClient(My.MySettings.Default.SMTPAddress)

 

            ' Add credentials

            client.UseDefaultCredentials = True

 

            ' send message

            client.Send(message)

 

            Return "Message sent to " + sendTo + " at " +

            DateTime.Now.ToString() + "."

 

        Catch ex As Exception

 

            Return ex.Message.ToString()

 

        End Try

 

    End Function

 

 

The next and last method contained in the Emailer class is used to validate an email address; this is accomplished by validating the address against a regular expression.

    ''' <summary>

    ''' Confirm that an email address is valid

    ''' in format

    ''' </summary>

    ''' <param name="emailAddress">Full email address to validate</param>

    ''' <returns>True if email address is valid</returns>

    Public Shared Function ValidateEmailAddress(ByVal emailAddress As String)

    As Boolean

 

        Try

 

            Dim TextToValidate As String = emailAddress

            Dim expression As New Regex("\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}")

 

            ' test email address with expression

            If (expression.IsMatch(TextToValidate)) Then

                ' is valid email address

                Return True

            Else

                ' is not valid email address

                Return False

            End If

 

        Catch ex As Exception

 

            Throw ex

 

        End Try

 

    End Function

 

 

End Class

 

Code:  frmTestEmailer.vb

This form class is the only class contained in the test application; it provides a simple interface for building an email message with attachments.

The class begins with the default imports. 

Imports System.Collections

Imports EmailHandler

 

The next section contains the class declaration. 

 

 

Public Class frmTestEmail

 

 

Following the class declaration, ArrayLists are declared; one is used to contain the attachments added to the email message and the other is used to contain a list of blind copy recipients.

 

    ''' <summary>

    ''' An arraylist containing

    ''' all of the attachments

    ''' </summary>

    Dim alAttachments As ArrayList

 

    ''' <summary>

    ''' An arraylist containing

    ''' all blind copy recipients

    ''' </summary>

    ''' <remarks></remarks>

    Dim alBlindCopies As ArrayList

 

The next bit of code in the application is the default constructor.

 

    ''' <summary>

    ''' Test Application Form:

    ''' This application is used to test sending

    ''' email and email with attachments.

    ''' </summary>

    Public Sub New()

 

        ' This call is required by the Windows Form Designer.

        InitializeComponent()

 

        ' Add any initialization after the InitializeComponent() call.

 

    End Sub

 

 

The next bit of code is the button click event handler used to add attachments to the email message.  The code adds the file or files selected from an open file dialog to the array list used to store the file paths temporarily; the contents of the array list as written out and displayed in a text box contained on the form.

 

 

    ''' <summary>

    ''' Add attachments to an email message

    ''' </summary>

    ''' <param name="sender"></param>

    ''' <param name="e"></param>

    ''' <remarks></remarks>

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles btnAdd.Click

        If (openFileDialog1.ShowDialog() = DialogResult.OK) Then

 

            Try

 

                Dim arr() As String = openFileDialog1.FileNames

                alAttachments = New ArrayList()

                txtAttachments.Text = String.Empty

                alAttachments.AddRange(arr)

 

                Dim s As String

                For Each s In alAttachments

                    txtAttachments.Text += s + "; "

                Next

 

            Catch ex As Exception

 

                MessageBox.Show(ex.Message, "Error")

 

            End Try

 

        End If

 

    End Sub

 

 

That next method contained in the class is used to exit the application.  It is the button click event handler for the cancel button.

 

    ''' <summary>

    ''' Exit the application when the user

    ''' selects the cancel button

    ''' </summary>

    ''' <param name="sender"></param>

    ''' <param name="e"></param>

    ''' <remarks></remarks>

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles btnCancel.Click

 

        Application.Exit()

 

    End Sub

 

       

The last method contained in the test application form is used to send the email message.  This is the button click event handler for the send button.  This method verifies that all of the required elements of the message are available and then uses those form fields to supply each of the arguments used in the SendMessage or SendMessageWithAttachments methods contained in the Emailer class.  Based upon whether or not the message contains any attachments, the appropriate method is called.

 

    ''' <summary>

    ''' Send an email message with or without attachments

    ''' </summary>

    ''' <param name="sender"></param>

    ''' <param name="e"></param>

    ''' <remarks></remarks>

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles btnSend.Click

 

        ' check for missing form fields

 

        ' check for a missing recipient email address

        If (String.IsNullOrEmpty(txtSendTo.Text)) Then

            MessageBox.Show("Missing recipient address.", "Email Error")

            Return

        End If

 

        ' check for a missing sender email address

        If (String.IsNullOrEmpty(txtSendFrom.Text)) Then

            MessageBox.Show("Missing sender address.", "Email Error")

            Return

        End If

 

        ' check for a missing subject line

        If (String.IsNullOrEmpty(txtSubjectLine.Text)) Then

            MessageBox.Show("Missing subject line.", "Email Error")

            Return

        End If

 

        ' check for a missing email message

        If (String.IsNullOrEmpty(txtMessage.Text)) Then

            MessageBox.Show("Missing message.", "Email Error")

            Return

        End If

 

        ' split the attachments list on the semi-colon

        ' to get an array of file paths

        Dim arr() As String = txtAttachments.Text.Split(";")

 

        ' populate the attachments list

        alAttachments = New ArrayList()

        Dim i As Integer

        For i = 0 To arr.Length - 1

            If (String.IsNullOrEmpty(arr(i).ToString().Trim()) = False) Then

                alAttachments.Add(arr(i).ToString().Trim())

            End If

        Next i

 

        ' split the blind copies list on the semi-colon

        ' to get an array of file paths

        Dim arrBcc() As String = txtBCC.Text.Split(";")

 

        ' populate the blind copy recipient list

        alBlindCopies = New ArrayList()

        For i = 0 To arrBcc.Length - 1

            If (String.IsNullOrEmpty(arrBcc(i).ToString().Trim()) = False)

            Then

                alBlindCopies.Add(arrBcc(i).ToString().Trim())

            End If

        Next i

 

        ' send the message, attachments (if any) and add

        ' the blind copies (if any)

        If (alAttachments.Count > 0 And _

            alBlindCopies.Count > 0) Then

            Dim result As String = _

                Emailer.SendEmailMessage(txtSendTo.Text, _

                txtSendFrom.Text, txtSubjectLine.Text, txtMessage.Text, _

                alBlindCopies, alAttachments)

 

            MessageBox.Show(result, "Email Transmittal")

 

        ElseIf (alAttachments.Count > 0 And _

                alBlindCopies.Count <= 0) Then

            Dim result As String = _

            Emailer.SendEmailMessage(txtSendTo.Text, _

                txtSendFrom.Text, txtSubjectLine.Text, txtMessage.Text, _

                Nothing, alAttachments)

            MessageBox.Show(result, "Email Transmittal")

 

        ElseIf (alAttachments.Count <= 0 And _

            alBlindCopies.Count > 0) Then

            Dim result As String = _

            Emailer.SendEmailMessage(txtSendTo.Text, _

                txtSendFrom.Text, txtSubjectLine.Text, txtMessage.Text, _

                alBlindCopies, Nothing)

            MessageBox.Show(result, "Email Transmittal")

 

        Else

            Dim result As String = _

                Emailer.SendEmailMessage(txtSendTo.Text, _

                txtSendFrom.Text, txtSubjectLine.Text, txtMessage.Text)

 

            MessageBox.Show(result, "Email Transmittal")

 

        End If

 

    End Sub

 

 

End Class

 

Summary

The article provide a simple description of the process required to send an email message with our without attachments.  Further, the procedure for configuring IIS and the STMP mail server is described.

 

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor