Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

VB.NET 2005 SMTP Email Message

3.50/5 (25 votes)
11 Jul 2006CPOL 1   2.4K  
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)