Sending Email via SMTP and C# SmtpClient Class

In this tutorial we will look at constructing an email message, adding attachments and sending email using the SMTP protocol.

By Tim Trott | C# ASP.Net MVC | March 16, 2010

There are a million and one reasons why you would need to send an email from within C# code - from sending an order confirmation through to error reporting.

SMTP stands for Simple Mail Transfer Protocol and is an Internet standard for email transmission across Internet Protocol (IP) networks. You will need a server address (either a name or IP address) that will be used to relay the message. This can be defined within the web.config if you are using ASP.Net or you can specify it within your code.

A minimal email message contains the following properties:

  1. To
  2. From
  3. Subject
  4. Body

Sending Email via SMTP and C#

So let's have a look at these and send a very simple email.

C#
MailMessage message = new MailMessage();
message.To.Add(new MailAddress("somebody@test.com"));
message.From.Add( = new MailAddress("my.address@sample.com"));
message.Subject = "This is a test email";
message.Body = "This is a quick test email to demonstrate sending emails from .Net";

Now all we need to do is connect to an SMTP server and send the message.

C#
SmtpClient client = new SmtpClient("SERVERNAME");
client.Send(message);

That's all there is to it!

Sending Formatted HTML Email Messages

If you try the test code on your server and get a sample message you will probably notice that the message isn't very attractive. But there is a solution! You can use HTML in the message body and CSS to change the style - fonts, colours, images, tables and more!

It's really easy to do, and here's how:

C#
message.Body = "This is my test <span style="color:blue">html</span> message.

";
message.IsBodyHtml = true;

Sending Emails with SMTP Attachments

Let's send an email with an attachment, just because it's such a common task - PDFs, invoices, embed images, hundreds of uses!

We need to use the Attachment class and change a few properties. Don't worry, they are really easy.

C#
string filename = "c:new foldermyfile.jpg";

Attachment emailAttachment = new Attachment(filename);
emailAttachment.ContentDisposition.Inline = false;
emailAttachment.ContentDisposition.DispositionType = DispositionTypeNames.Attachment;
emailAttachment.ContentType.MediaType = "text/jpeg";
emailAttachment.ContentType.Name = Path.GetFileName(filename);
message.Attachments.Add(emailAttachment);

If you have more than one attachment you can use a loop over a list of filenames.

Sending Emails to Multiple Recipients with SmtpClient

In the example above we sent an email to one email address, but it is quite easy to send the email to multiple addresses. All you need to do is repeat the call to message.To.Add(...) for as many email addresses as you want.

C#
string emailList = "test1@test.com,test2@test.com,test3@testers.com";

string[] toAddresses = emailList.Split(',');
foreach (string address in toAddresses)
  message.To.Add(new MailAddress(address));

This works for the CC and BCC as well.

Sending Priority Messages

You can also mark a mail message as having a high priority (typically an exclamation mark icon next to the message in the inbox).

C#
message.Priority = MailPriority.High;

Other properties of the MailMessage class

There are several other useful properties of the MailMessage class which you can utilise; this is a list of the most common.

  1. CC - send a carbon copy to recipients.
  2. BCC - send an anonymous carbon copy to recipients.
  3. ReplyTo - change the reply-to address for the message.

ASP.Net SMTP Configuration

When you define an instance of SmtpClient class you can pass in the server name as a parameter, but you can also store this in the web.config file and use an empty constructor. This aids the scalability and distribution of your web application.

xml
<system.net>
  <mailSettings>
    <smtp>
      <network host="SERVERNAME"/>
    </smtp>
  </mailSettings>
</system.net>
C#
SmtpClient client = new SmtpClient();
Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has 2 comment(s). Why not join the discussion!

We respect your privacy, and will not make your email public. Learn how your comment data is processed.

  1. VK

    On Tuesday 1st of October 2019, Vladan M. Kostic said

    Hi, in my work I used JMSG and MSG .NET library.
    There are a number of use cases on the site and the API itself is well documented.
    Link is http://www.independentsoft.de/jmsg/index.html and http://www.independentsoft.de/msg/index.html

  2. JE

    On Monday 20th of January 2014, jeryymanly said

    You should add network credentials to send email

    C#
    SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
    
     SmtpServer.EnableSsl = true;

    source: http://csharp.net-informations.com/communications/csharp-smtp-mail.htm

    c# email