Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

SMTP Client Class

4.33/5 (7 votes)
27 Oct 2014CPOL 13.4K   361  
This is a simple VB.NET class that encapsulates the sending of e-mail messages via SMTP.

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. 

VB.NET
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:

VB.NET
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)