Introduction
This class shows how to use the .NET SMTPClient and MailMessage classes and provides a simple interface for sending e-mail messages. It is designed primarily for sending single messages rather than bulk e-mail, and therefore is ideal for applications such as sending notification messages from an application or web site.
Using the code
The class can be dropped into any VB.NET project as is and includes the required references. The simplest usage of this class is shown below. Using the Send
method below will automatically set the body of the message to use HTML format if the body starts with an HTML tag and also contains any closing HTML tag.
Dim message As SMTPSender = New SMTPSender("mail.mydomain.com", "from@mydomain.com", "password", True)
message.Send("from@domain.com", "From Name", "to@domain.com", "To Name", "Subject", "<p>The Body</p>")
There is also an overloaded Send where both priority and HTML format can be specified explicitly, but it requires a call to SetRecipient
or SetRecipients
:
Dim message As SMTPSender = New SMTPSender("mail.mydomain.com", "from@mydomain.com", "password", True)
message.SetRecipient("to@domain.com", "To Name")
message.Send("from@domain.com", "From Name", "Subject", "The Body", MailPriority.High, True)
History
2014-10-27: Original posting.