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
#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
#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
#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!