RSS Feed

Practical Programming: Sending Email

Posted on Friday, January 16, 2009 in Practical Programming

By Derek Hatchard

Derek_Hatchard Sending email from a .NET application is incredibly simple:

using System.Net.Mail;

SmtpClient _smtp = new SmtpClient("smtp.test.com");
_smtp.Send(""
           ""
           "Subject"
           "Body");

With just these few lines of code, you are sending an email from to . Pretty easy stuff.

But what if you need something a little more complicated? The example above assumes the standard SMTP port for email with no authentication and an unencrypted connection. Fortunately these “complications” are quite easy to deal with in .NET.


Different Port

If you need to send email on a different port, simply add the following line:

_smtp.Port = 587;

SMTP Authentication

If you are not sending through a local SMTP server, your app will likely need to authenticate itself. If you need to pass credentials to the SMTP server, use the Credentials property on the SmtpClient class:

using System.Net;

_smtp.Credentials = new NetworkCredential("username", "pwd");

Encrypted Connection

Since you are passing credentials to the server, you really should be using a secure connection (many mail providers require you to use a secure connection). In code, it’s just one line:

_smtp.EnableSsl = true;

Sending Email On Behalf of Someone

A final practical consideration when sending email is the distinction between From and Sender. In a simple scenario, you only need a From address. But if you are writing code to send email, chances are good that there is something not-quite-so-simple happening.

Perhaps you have received an email with a header like this in Outlook:

fromsender

This is an example of one email account sending an email on behalf of someone else. This is often used by automated systems and it is trivial to add to your application. To add a Sender you cannot pass values directly to an SmtpClient object; you must create a new instance of the MailMessage class, set the properties, and then pass the MailMessage object to the SmtpClient object:

MailMessage _msg = new MailMessage(""
                                   "",  
                                   "Subject",  
                                   "Body"); 
_msg.Sender = new MailAddress("");
_smtp.Send(_msg);

Keep in mind that your message is going through an SMTP server that might not let you set arbitrary From and Sender values.

Bringing it All Together

Here is what the final code looks like that incorporates an alternate port, credentials, encrypted transport, and a sender:

using System.Net;
using System.Net.Mail;

SmtpClient _smtp = new SmtpClient("smtp.test.com");
_smtp.Port = 587;
_smtp.Credentials = new NetworkCredential("username", "pwd");
_smtp.EnableSsl = true;

MailMessage _msg = new MailMessage("",
                                   "",
                                   "Subject",
                                   "Body");
_msg.Sender = new MailAddress("");
_smtp.Send(_msg);

—-

Derek Hatchard is the content editor for http://microsoft.com/youshapeit/msdn and https://devshaped.com. He is also the founder of Crowd Space. You can find him online at http://derekh.com, http://ardentdev.com, and .

Bring on the comments

  1. says:

    Great post Derek,

    You can also provide the settings for your SmptClient using the following configuration section.







    In your sample it replaces the first 4 lines of code with:

    SmtpClient _smtp = new SmtpClient();
    _smtp.EnableSsl = true;

  2. waqas says:

    i tried this example but the mail sent as not on behalf. i mean only one email address was shown in the from when i opened that mail. although i receieved many emails which showing other email address in from field using on behalf of. kindly help me

`