Send an Email via SMTP

9/26/2010 7:39:53 PM
SMTP stands for Simple Mail Transport Protocol and is a simple, well-defined protocol for exchanging emails with an SMTP server. Thankfully, the specifics of the protocol are handled for you in the System.Net.Mail.SmtpClient class. Figure 1 shows an SMTP client.
Figure 1. It’s very easy to create a simple SMTP client with full attachment support.

To see a full example of a simple email program, look at the EmailClient code example for this chapter. Here is just the method for sending email:

private void SendEmail(string host, int port,
string username, string password,
string from, string to,
string subject, string body,
ICollection attachedFiles)
//A MailMessage object must be disposed!
using (MailMessage message = new MailMessage())
message.From = new MailAddress(from);
message.Subject = subject;
message.Body = body;
foreach (string file in attachedFiles)
message.Attachments.Add(new Attachment(file));

SmtpClient client = new SmtpClient(host, port);
//if your SMTP server requires a password,
//the following line is important
client.Credentials = new NetworkCredential(username, password);
//this send is synchronous. You can also choose
//to send asynchronously


The MailMessage class must be disposed after you send it. I once noticed strange problems with mail not going out immediately when I neglected to do this.

Most View
Décor Worthy REL Acoustics Habitat1 Subwoofer (part 1)
Sony NEX-6 Mirrorless Camera Review (Part 8)
BlackBerry Development : Determining the Best Approach
Group Test: AMD FX-8350, FX-8320, FX-6300 And FX-4300 - All Vishera Processors (Part 5)
Compact Liquid Cooling Systems Roundup – Front Runners (Part 3)
That’s Online Entertainment (Part 3)
Dual-channel DDR3 RAM (Part 3)
CD Players Awards – Q1 2013 (Part 1)
Sharepoint 2013 : Introducing jQuery for SharePoint developers (part 2) - Understanding jQuery methods,Understanding jQuery event handling
Windows 7 : Installing and Removing Hardware (part 6) - Removing Hardware, Updating Drivers, Dealing with Devices that Prevent Windows 7 from Starting
Top 10
Sharepoint 2013 : Farm Management - Disable a Timer Job,Start a Timer Job, Set the Schedule for a Timer Job
Sharepoint 2013 : Farm Management - Display Available Timer Jobs on the Farm, Get a Specific Timer Job, Enable a Timer Job
Sharepoint 2013 : Farm Management - Review Workflow Configuration Settings,Modify Workflow Configuration Settings
Sharepoint 2013 : Farm Management - Review SharePoint Designer Settings, Configure SharePoint Designer Settings
Sharepoint 2013 : Farm Management - Remove a Managed Path, Merge Log Files, End the Current Log File
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered