Click here to Skip to main content
Click here to Skip to main content

AdHawkMailer - An ASP.NET Component for Sending Emails in VB.NET

, 16 Jun 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
AdHawkMailer is an ASP.NET component for sending emails in VB.NET.

Sample Image - maximum width is 600 pixels

Introduction

Nowadays, sites communicate with their users via emails, newsletters, promotional offers, status updates, and so on. This small class can be dropped into any App_Code folder or class library, and will give you the ability to programmatically send email. I am using an Object Oriented Design as it will make it very easy to implement and modify.

Background

The idea was to make a reusable component, in this case, an Emailer, if you will. Something I can drop in and go. I have used the System.Net.Mail namespace in the past and thought it was great, but most of the time, it's pretty much the same code. So, I decided to break it down into OOP and build a reusable class.

The Message object has the following properties:

  • From address - String
  • To address - String
  • Is body HTML - Boolean
  • Subject - String
  • Body - String
  • Attachments - ArrayList of file paths.
  • SMTP address - String
  • Port - Integer
  • Username - String
  • Password - String
  • SSL enabled - Boolean

Using the code

Here is a very simple example of using the code. I'm importing the namespace at the top, and I'm sending an email on a button click event. However, here I am only assigning a To address because I have everything else set in the default constructor.

Imports AdHawkMailer

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles Button1.Click

        Dim objMessage As New Message
        objMessage.ToAddress = "adam.n.thompson@gmail.com"

        Dim mailSent As Boolean = objMessage.Send(objMessage)

    End Sub
End Class

Here is the class:

Imports Microsoft.VisualBasic
Imports System.Net.Mail
Imports System

Namespace AdHawkMail

Public Class Message

''' <span class="code-SummaryComment"><summary></span>
''' These are the properties needed for SMTP email communication. 
''' Most of them are self explainitory.
''' ==================================================================
''' FromAddress - Sets the from address that will show on the email sent
''' ToAddress - The Address of the Recipiant
''' IsBodyHtml - True if the body is going to be html / False otherwise
''' Subject - The Subject of the email
''' Body - Email body text/html
''' Attachments = You can send a collection of attachments
''' SmtpAddress - The IP address or URL of the smtp server you will be sending from
''' Port - The port number you will be using 
''' UserName - If authentication is required
''' Password - If authentication is required
''' SslEnabled - If ssl is required
''' <span class="code-SummaryComment"></summary></span>
''' <span class="code-SummaryComment"><remarks></remarks></span>
#Region "Private Members and Public Properties"

    Private _fromAddress As String
    Private _toAddress As String
    Private _isBodyHtml As Boolean
    Private _subject As String
    Private _body As String
    Private _smtpAddress As String
    Private _port As Integer
    Private _userName As String
    Private _password As String
    Private _sslEnabled As Boolean
    Private _attachments As ArrayList

    Public Property FromAddress() As String

        Get
            Return _fromAddress
        End Get

        Set(ByVal value As String)
            _fromAddress = value
        End Set

    End Property

    Public Property ToAddress() As String

        Get
            Return _toAddress
        End Get

        Set(ByVal value As String)
            _toAddress = value
        End Set

    End Property

    Public Property IsBodyHtml() As Boolean

        Get
            Return _isBodyHtml
        End Get

        Set(ByVal value As Boolean)
            _isBodyHtml = value
        End Set

    End Property

    Public Property Subject() As String

    Get
        Return _subject
    End Get

    Set(ByVal value As String)
        _subject = value
    End Set

    End Property

    Public Property Body() As String

        Get
            Return _body
        End Get

        Set(ByVal value As String)
            _body = value
        End Set

    End Property

    Public Property SmtpAddress() As String

        Get
            Return _smtpAddress
        End Get

        Set(ByVal value As String)
            _smtpAddress = value
        End Set

    End Property

    Public Property Port() As Integer

        Get
            Return _port
        End Get

        Set(ByVal value As Integer)
            _port = value
        End Set

    End Property

    Public Property UserName() As String

        Get
            _userName
        End Get

        Set(ByVal value As String)
            _userName = value
        End Set

    End Property

    Public Property Password() As String

        Get
            Return _password
        End Get

        Set(ByVal value As String)
            _password = value
        End Set

    End Property

    Public Property SslEnabled() As Boolean

        Get
            Return _sslEnabled
        End Get

        Set(ByVal value As Boolean)
            _sslEnabled = value
        End Set

    End Property

#End Region

''' <span class="code-SummaryComment"><summary></span>
''' This constructor is where you would set the SMTP configuration,
''' and anything that you know will not be changing. 
''' You can override this constructor as well to allow for multiple
''' configurations
''' <span class="code-SummaryComment"></summary></span>
''' <span class="code-SummaryComment"><remarks></remarks></span>

#Region "Constructors"

    Public Sub New()

        FromAddress = "server@exadev.com"
        ToAddress = "darrylfluhart@yahoo.com"
        IsBodyHtml = True
        Subject = ""
        Body = ""
        SmtpAddress = "mail.exadev.com"
        Port = 25
        UserName = Nothing
        Password = Nothing
        SslEnabled = False

    End Sub

#End Region

''' <span class="code-SummaryComment"><summary></span>
''' This is the send Method. it takes a message object. 
''' <span class="code-SummaryComment"></summary></span>
''' <span class="code-SummaryComment"><param name="_emailMessage"></span>
''' Some of the properties are optional 
'''=================================================================
''' Attachments - The code will check to see if you have any attachment
''' in the arrayList
''' UserName - if authentication is not required set the value to Nothing
''' Password - if authentication is not required set the value to Nothing
''' <span class="code-SummaryComment"></param></span>
''' <span class="code-SummaryComment"><returns>True if the message was sent, and false otherwise</returns></span>
''' <span class="code-SummaryComment"><remarks></remarks></span>

#Region "Methods"

    Public Sub AddAttachment(ByVal attachment As String)

        _attachments.Add(attachment)

    End Sub

    Public Sub RemoveAttachment(ByVal attachment As String)

        _attachments.Remove(attachment)

    End Sub

    Public Function Send(ByVal _emailMessage As Message) As Boolean

        Dim mailSent As Boolean

        Try
            Dim Mail As New System.Net.Mail.MailMessage()

            With Mail
                .To.Add(_emailMessage.ToAddress)
                .From = New MailAddress(_emailMessage.FromAddress)
                .Subject = _emailMessage.Subject
                .Body = _emailMessage.Body
                .IsBodyHtml = _emailMessage.IsBodyHtml

                If Not _attachments.Count = 0 Then
                    Dim i As Integer = 0

                    For Each objAttachment As Object In _attachments
                        .Attachments.Add(_attachments(i))
                        i += 1
                    Next
                End If

            End With

            Dim SMTP As New SmtpClient(_emailMessage.SmtpAddress)

            If Not _emailMessage.UserName = Nothing _
                And _emailMessage.Password = Nothing Then
                SMTP.Credentials = New Net.NetworkCredential _
                (_emailMessage.UserName, _emailMessage.Password)

            End If

            SMTP.Port = _emailMessage.Port
            SMTP.Host = _emailMessage.SmtpAddress
            SMTP.EnableSsl = _emailMessage.SslEnabled
            SMTP.Send(Mail)
            SMTP = Nothing
            Mail.Dispose()
            mailSent = True

        Catch ex As Exception
            mailSent = False
        End Try

        Return mailSent

    End Function
#End Region

End Class

End Namespace

Happy coding!

License

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

Share

About the Author

AdamNThompson
Web Developer
United States United States
No Biography provided
Follow on   Twitter

Comments and Discussions

 
GeneralThanks! PinmemberPsydefx15-Mar-08 7:46 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 16 Jun 2008
Article Copyright 2007 by AdamNThompson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid