WelcomeServicesPortfolioKnowledge BaseContact Us
27 Seconds, Inc.


Knowledge base View Article

need hosting?

Need hosting? We use and love our host - CrystalTech.com!
Need hosting? We use and love our host - CrystalTech.com!

Text Disguise - CAPTCHA-image/Human Interactive Proof web services

National Holiday Dates and Bank Holiday Dates - web site that provides the dates of national and bank holidays for the United States, United Kingdom, Ireland and Scotland

Holiday Web Service - web services for programmers to provide the dates of national and bank holidays in their applications for the United States, United Kingdom, Ireland and Scotland

Our Family Heart - web site to help families communicate no matter where they are

What?! You haven't heard about the greatest remote controlled flyer to come out in years?! Checkout the FlyTech DragonFly now!

Easily Send Email from .NET Applications

Summary: This shared/static class allows you to quickly easily send an email from any .NET application (web forms/ASP.NET and windows forms applications). Supports sending to one or more recipients and with or without a file attachment.
 
Keywords: ATTACHMENT
EMAIL
MAIL HOST
MAIL SERVER
MAILMESSAGE
SEND
SENDEMAIL
SMTP
SMTPMAIL
Applicable Software: Active Server Pages .NET (ASP.NET)
Visual Basic .NET (VB.NET)
 
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")

 
Author: Douglas L. Setzer, II, http://www.27seconds.com
Posted On: 12/21/2005 2:08:20 PM

Rate this article: Average: 4.3
n/a12345678910
Comments?

Article Search   |   All Articles

 

 
©2002 27 Seconds, Inc. All Rights Reserved.