Option Explicit On Option Strict Off Option Compare Text '============================================================================================================= ' ' cEmail.vb ' --------- ' ' Created By : Kevin Wilson ' http://www.TheVBZone.com ( The VB Zone ) ' http://www.TheVBZone.net ( The VB Zone .net ) ' ' Last Update : July 23, 2003 ' Created On : July 23, 2003 ' ' VB Versions : VB.NET 1.1 (VS.NET 2003) ' ' Requires : The .NET Framework v1.1 ' (Reference System.Web.dll) ' ' Description : This class module makes it easy to send an Email via the use of an SMTP server. Note that ' You can use IIS's virtual SMTP as well as a dedicated SMTP server. ' ' Example Use : ' ' Dim intErrNum As Integer ' Dim strErrSrc As String ' Dim strErrDesc As String ' If cEmail.SendEmail("mail.MyCompany.com", "me@mycompany.com", "you@yourcompany.com", "Hello World!", "This is just a test Email", Web.Mail.MailPriority.High, Web.Mail.MailFormat.Text, intErrNum, strErrSrc, strErrDesc) = False Then ' MsgBox("The following error occured while trying to send the Email:" & Chr(13) & Chr(13) & "Error Number = " & intErrNum.ToString & Chr(13) & "Error Source = " & strErrSrc & Chr(13) & "Error Description = " & strErrDesc, MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, " Error") ' Else ' MsgBox("Successfully sent Email!", MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, " ") ' End If ' '============================================================================================================= ' ' LEGAL: ' ' You are free to use this code as long as you keep the above heading information intact and unchanged. Credit ' given where credit is due. Also, it is not required, but it would be appreciated if you would mention ' somewhere in your compiled program that that your program makes use of code written and distributed by ' Kevin Wilson (www.TheVBZone.com). Feel free to link to this code via your web site or articles. ' ' You may NOT take this code and pass it off as your own. You may NOT distribute this code on your own server ' or web site. You may NOT take code created by Kevin Wilson (www.TheVBZone.com) and use it to create products, ' utilities, or applications that directly compete with products, utilities, and applications created by Kevin ' Wilson, TheVBZone.com, or Wilson Media. You may NOT take this code and sell it for profit without first ' obtaining the written consent of the author Kevin Wilson. ' ' These conditions are subject to change at the discretion of the owner Kevin Wilson at any time without ' warning or notice. Copyright© by Kevin Wilson. All rights reserved. ' '============================================================================================================= Imports System.Web.Mail Imports System.Web.Mail.MailMessage Public Class cEmail #Region "Property Variables" Private m_EmailSMTP As String Private m_EmailFROM As String Private m_EmailTO As String Private m_EmailCC As String Private m_EmailBCC As String Private m_EmailSubject As String Private m_EmailBody As String Private m_EmailAttachments As String Private m_EmailFormat As MailFormat Private m_EmailPriority As MailPriority #End Region #Region "Constructors" Public Sub New(Optional ByVal sSmtpServer As String = "", _ Optional ByVal sFROM As String = "", _ Optional ByVal sTO As String = "", _ Optional ByVal sCC As String = "", _ Optional ByVal sBCC As String = "", _ Optional ByVal sSUBJECT As String = "", _ Optional ByVal sBODY As String = "", _ Optional ByVal sAttachments As String = "", _ Optional ByVal Format As MailFormat = MailFormat.Text, _ Optional ByVal Priority As MailPriority = MailPriority.Normal) '// Initialize properties m_EmailSMTP = sSmtpServer m_EmailFROM = sFROM m_EmailTO = sTO m_EmailCC = sCC m_EmailBCC = sBCC m_EmailSubject = sSUBJECT m_EmailBody = sBODY m_EmailAttachments = sAttachments m_EmailFormat = Format m_EmailPriority = Priority End Sub #End Region #Region "Deconstructors" Protected Overrides Sub Finalize() MyBase.Finalize() End Sub #End Region #Region "Public Properties" Public Property SmtpServer() As String Get Return m_EmailSMTP End Get Set(ByVal Value As String) m_EmailSMTP = Value End Set End Property Public Property AddressFROM() As String Get Return m_EmailFROM End Get Set(ByVal Value As String) m_EmailFROM = Value End Set End Property '// This can be a semicolon (;) delimited list of Email addresses Public Property AddressTO() As String Get Return m_EmailTO End Get Set(ByVal Value As String) m_EmailTO = Value End Set End Property '// This can be a semicolon (;) delimited list of Email addresses Public Property AddressCC() As String Get Return m_EmailCC End Get Set(ByVal Value As String) m_EmailCC = Value End Set End Property '// This can be a semicolon (;) delimited list of Email addresses Public Property AddressBCC() As String Get Return m_EmailBCC End Get Set(ByVal Value As String) m_EmailBCC = Value End Set End Property Public Property Subject() As String Get Return m_EmailSubject End Get Set(ByVal Value As String) m_EmailSubject = Value End Set End Property '// If Format = MailFormat.Text, then this can be HTML code. Otherwise, must be PLAIN TEXT. Public Property Body() As String Get Return m_EmailBody End Get Set(ByVal Value As String) m_EmailBody = Value End Set End Property '// "Attachments" property is a semicolon (;) delimited list of file paths, or just one file path Public Property Attachments() As String Get Return m_EmailAttachments End Get Set(ByVal Value As String) m_EmailAttachments = Value End Set End Property Public Property Format() As MailFormat Get Return m_EmailFormat End Get Set(ByVal Value As MailFormat) m_EmailFormat = Value End Set End Property Public Property Priority() As MailPriority Get Return m_EmailPriority End Get Set(ByVal Value As MailPriority) m_EmailPriority = Value End Set End Property #End Region #Region "Public Methods" '// Sends an Email based on the properties of this class '// NOTE: All addresses (except FROM) can be a semicolon (;) delimited list of Email addresses Public Function SendEmailEx(Optional ByRef Return_ErrNum As Integer = 0, _ Optional ByRef Return_ErrSrc As String = "", _ Optional ByRef Return_ErrDesc As String = "") As Boolean Return SendEmail(m_EmailSMTP, m_EmailFROM, m_EmailTO, m_EmailCC, m_EmailBCC, m_EmailSubject, m_EmailBody, m_EmailAttachments, m_EmailPriority, m_EmailFormat, Return_ErrNum, Return_ErrSrc, Return_ErrDesc) End Function '// Sends an Email based on the specified parameters '// NOTE: All addresses (except FROM) can be a semicolon (;) delimited list of Email addresses Public Overloads Shared Function SendEmail(ByVal sSmtpServer As String, _ ByVal sFROM As String, _ ByVal sTO As String, _ ByVal sSUBJECT As String, _ ByVal sBODY As String, _ Optional ByVal Priority As MailPriority = MailPriority.Normal, _ Optional ByVal Format As MailFormat = MailFormat.Text, _ Optional ByRef Return_ErrNum As Integer = 0, _ Optional ByRef Return_ErrSrc As String = "", _ Optional ByRef Return_ErrDesc As String = "") As Boolean Return SendEmail(sSmtpServer, sFROM, sTO, "", "", sSUBJECT, sBODY, Nothing, Priority, Format, Return_ErrNum, Return_ErrSrc, Return_ErrDesc) End Function '// Sends an Email based on the specified parameters '// NOTE: All addresses (except FROM) can be a semicolon (;) delimited list of Email addresses '// NOTE: "Attachments" parameter is a semicolon (;) delimited list of file paths, or just one file path Public Overloads Shared Function SendEmail(ByVal sSmtpServer As String, _ ByVal sFROM As String, _ ByVal sTO As String, _ ByVal sCC As String, _ ByVal sBCC As String, _ ByVal sSUBJECT As String, _ ByVal sBODY As String, _ Optional ByVal Attachments As String = "", _ Optional ByVal Priority As MailPriority = MailPriority.Normal, _ Optional ByVal Format As MailFormat = MailFormat.Text, _ Optional ByRef Return_ErrNum As Integer = 0, _ Optional ByRef Return_ErrSrc As String = "", _ Optional ByRef Return_ErrDesc As String = "") As Boolean On Error GoTo ErrorTrap Dim oMail As New MailMessage Dim arrAttachments() As String Dim intCounter As Integer '// Set default values Return_ErrNum = 0 Return_ErrSrc = "" Return_ErrDesc = "" '// Validate parameters If sSmtpServer.Trim = "" Then Err.Raise(-1, "cEmail.SendEmail()", "No SMTP server specified to send the Email") ElseIf sTO.Trim = "" Then Err.Raise(-1, "cEmail.SendEmail()", "No TO address specified to send the Email to") ElseIf sSUBJECT = "" And sBODY = "" Then Err.Raise(-1, "cEmail.SendEmail()", "Neither the SUBJECT nor the BODY of the Email was specified") End If '// Setup the Email object's properties With oMail .From = sFROM .To = sTO .Cc = sCC .Bcc = sBCC .Subject = sSUBJECT .Body = sBODY .BodyFormat = Format .Priority = Priority If Attachments <> "" Then If InStr(Attachments, ";") <= 0 Then If System.IO.File.Exists(Attachments) = True Then .Attachments.Add(New MailAttachment(Attachments)) End If Else arrAttachments = Attachments.Split(";") For intCounter = arrAttachments.GetLowerBound(0) To arrAttachments.GetUpperBound(0) If System.IO.File.Exists(arrAttachments(intCounter)) = True Then .Attachments.Add(New MailAttachment(arrAttachments(intCounter))) End If Next End If End If End With '// Send the Email via the specified SMTP server SmtpMail.SmtpServer = sSmtpServer SmtpMail.Send(oMail) '// Clean up oMail = Nothing Return True ErrorTrap: Return_ErrNum = Err.Number Return_ErrSrc = Err.Source Return_ErrDesc = Err.Description Err.Clear() oMail = Nothing End Function #End Region End Class