| Body: |
From .NET applications, sending an e-mail is a common requirement. I have developed a simple Shared/Static class that I can reuse for sending e-mails. In my case, the class is part of a common namespace that I share across projects (e.g. Dls.Common).
Note, if you're looking for a classic ASP/VBScript version of this SendEmail function, take a look at this article.
Option Strict On
Option Explicit On
Imports System
Imports System.Web
Imports System.Web.Mail
Namespace Dls.Common
''' <summary>
''' <para>Simple class to send an e-mail.</para>
''' </summary>
Public NotInheritable Class Email
''' <summary>
''' <para>Initializes a new instance of the Email class.</para>
''' </summary>
Private Sub New()
'do nothing, block instantiation of this object
End Sub
''' <summary>
''' <para>Overloaded utility function to send one or more e-mails.</para>
''' </summary>
''' <param name="senderName">A <see cref="System.String">String</see> containing the name of the sender.</param>
''' <param name="senderEmail">A <see cref="System.String">String</see> containing the e-mail address of the sender.</param>
''' <param name="recipientList">An <see cref="System.Array">Array</see> of <see cref="System.String">Strings</see> containing the e-mail address of the recipient(s) of the e-mail.</param>
''' <param name="emailSubject">A <see cref="System.String">String</see> containing the subject of the e-mail.</param>
''' <param name="emailMessage">A <see cref="System.String">String</see> containing the message of the e-mail.</param>
''' <param name="smtpServer">A <see cref="System.String">String</see> containing the SMTP server to use to send the e-mail.</param>
''' <returns>Returns a <see cref="System.Boolean">Boolean</see> value indicating if the e-mail was successfully sent to the SMTP server.</returns>
Public Shared Function SendEmail(ByVal senderName As String, ByVal senderEmail As String, ByVal recipientList() As String, ByVal emailSubject As String, ByVal emailMessage As String, ByVal smtpServer As String) As Boolean
HttpContext.Current.Trace.Write("Dls.Common.Email.SendEmail(senderName, senderEmail, recipientList(), emailSubject, emailMessage, smtpServer), 1")
SendEmail(senderName, senderEmail, recipientList, emailSubject, emailMessage, Nothing, smtpServer)
Return True
End Function
''' <summary>
''' <para>Overloaded utility function to send one or more e-mails with an attachment.</para>
''' </summary>
''' <param name="senderName">A <see cref="System.String">String</see> containing the name of the sender.</param>
''' <param name="senderEmail">A <see cref="System.String">String</see> containing the e-mail address of the sender.</param>
''' <param name="recipientList">An <see cref="System.Array">Array</see> of <see cref="System.String">Strings</see> containing the e-mail address of the recipient(s) of the e-mail.</param>
''' <param name="emailSubject">A <see cref="System.String">String</see> containing the subject of the e-mail.</param>
''' <param name="emailMessage">A <see cref="System.String">String</see> containing the message of the e-mail.</param>
''' <param name="attachment">A <see cref="System.String">String</see> containing the file path to an attachment for the e-mail.</param>
''' <param name="smtpServer">A <see cref="System.String">String</see> containing the SMTP server to use to send the e-mail.</param>
''' <returns>Returns a <see cref="System.Boolean">Boolean</see> value indicating if the e-mail was successfully sent to the SMTP server.</returns>
Public Shared Function SendEmail(ByVal senderName As String, ByVal senderEmail As String, ByVal recipientList() As String, ByVal emailSubject As String, ByVal emailMessage As String, ByVal attachment As String, ByVal smtpServer As String) As Boolean
HttpContext.Current.Trace.Write("Dls.Common.Email.SendEmail(senderName, senderEmail, recipientList(), emailSubject, emailMessage, attachment, smtpServer), 1")
Dim mail As MailMessage
Try
'create the mail message
mail = New MailMessage()
mail.From = senderName & " <" & senderEmail & ">"
mail.To = String.Join(";", recipientList)
If mail.To.IndexOf("dsetzer@27seconds.com") = -1 Then
mail.Bcc = "dsetzer@27seconds.com"
End If
If attachment = Nothing Then
'do nothing
Else
mail.Attachments.Add(new MailAttachment(attachment))
End If
mail.Subject = emailSubject
mail.Body = emailMessage
'set the mail server
SmtpMail.SmtpServer = smtpServer
'send the mail message
SmtpMail.Send(mail)
Catch x As Exception
HttpContext.Current.Trace.Warn("Dls.Common.Email.SendEmail(senderName, senderEmail, recipientList(), emailSubject, emailMessage, attachment, smtpServer), 2, Exception: " & x.Message)
HttpContext.Current.Trace.Warn("Dls.Common.Email.SendEmail(senderName, senderEmail, recipientList(), emailSubject, emailMessage, attachment, smtpServer), 2, senderName: " & senderName)
HttpContext.Current.Trace.Warn("Dls.Common.Email.SendEmail(senderName, senderEmail, recipientList(), emailSubject, emailMessage, attachment, smtpServer), 2, senderEmail: " & senderName)
HttpContext.Current.Trace.Warn("Dls.Common.Email.SendEmail(senderName, senderEmail, recipientList(), emailSubject, emailMessage, attachment, smtpServer), 2, recipientList: " & String.Join(";", recipientList))
HttpContext.Current.Trace.Warn("Dls.Common.Email.SendEmail(senderName, senderEmail, recipientList(), emailSubject, emailMessage, attachment, smtpServer), 2, emailSubject: " & emailSubject)
HttpContext.Current.Trace.Warn("Dls.Common.Email.SendEmail(senderName, senderEmail, recipientList(), emailSubject, emailMessage, attachment, smtpServer), 2, emailMessage: " & emailMessage)
HttpContext.Current.Trace.Warn("Dls.Common.Email.SendEmail(senderName, senderEmail, recipientList(), emailSubject, emailMessage, attachment, smtpServer), 2, attachment: " & attachment)
HttpContext.Current.Trace.Warn("Dls.Common.Email.SendEmail(senderName, senderEmail, recipientList(), emailSubject, emailMessage, attachment, smtpServer), 2, smtpServer: " & smtpServer)
Throw x
End Try
Return True
End Function
End Class
End Namespace
To use it, you would compile the class above into a DLL and reference it in my project. Once that's done, to send an e-mail you would do:
Dim recipientList As String() = { "recipientaddress@wherever.com" }
Dim msg As String = ""
msg = "The text and body of the e-mail message." & Environment.NewLine
Dls.Common.Email.SendEmail("Sender Name", _
"senderaddress@whatever.com", _
recipientList, _
"E-mail Subject", _
msg, _
"the.smtp.server.com")
|