5,442,984 members and growing! (18,782 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Beginner

VB.Net 2005 SMTP Email Message

By Edacio

Send an SMTP mail message with multiple recipients and multiple file attachments
VB, Windows, .NET, Visual Studio, Dev

Posted: 19 Jun 2006
Updated: 11 Jul 2006
Views: 66,271
Bookmarked: 41 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
15 votes for this Article.
Popularity: 3.65 Rating: 3.10 out of 5
3 votes, 20.0%
1
1 vote, 6.7%
2
2 votes, 13.3%
3
5 votes, 33.3%
4
4 votes, 26.7%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

07-11-2006 Article and Source Updated

I needed to send an SMTP email to multiple recipients with multiple attachements in VB.net 2005. Sending an SMTP email in VB.net 2005 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 instanciate 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)
        '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 of the class you can config your SMTP Host inside of the Web.Config file.

  <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

        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 the SendEmailMessage is overloaded you can also 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)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Edacio


.Net Developer
Occupation: Web Developer
Location: United States United States

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralThe code executes, but no mail is received.memberJerodr9:31 4 Jun '08  
GeneralSmtpMail.Host = "10.10.10.10"memberbaruch_a3:15 28 Apr '08  
GeneralEmail ends up in Junk BoxmemberJared James Sullivan12:39 8 Oct '07  
GeneralInvalid URI: The format of the URI could not be determined.memberdino102223:47 28 Sep '07  
QuestionNot sending in timely mannermemberchetton20009:35 21 May '07  
QuestionUnable to send mailmemberMicha Brans2:15 26 Mar '07  
Generalerrormembershakti538521:22 10 Jan '07  
GeneralEncoding.Default ??memberHART211:18 5 Jul '06  
GeneralRe: Encoding.Default ??memberEdacio4:53 5 Jul '06  
GeneralRe: Encoding.Default ??memberHART215:30 5 Jul '06  
GeneralRe: Encoding.Default ??memberEdacio5:10 6 Jul '06  
GeneralRe: Encoding.Default ??memberHART219:07 6 Jul '06  
AnswerRe: Encoding.Default ??membereddybt@cantv.net9:00 20 Jul '07  
GeneralRe: Encoding.Default ??membermike_hoot3:33 26 Nov '07  
GeneralAuthorizationmemberzjst3:35 3 Jul '06  
GeneralRe: AuthorizationmemberEdacio4:47 3 Jul '06  
GeneralRe: Authorizationmemberzjst7:11 3 Jul '06  
GeneralRe: AuthorizationmemberEdacio4:58 6 Jul '06  
GeneralRe: AuthorizationmemberEdacio5:03 6 Jul '06  
GeneralRe: Authorizationmembermiglou8:24 22 Dec '06  
GeneralRe: Authorizationmemberdeadly_risk14:47 26 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Jul 2006
Editor:
Copyright 2006 by Edacio
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project