Learning never exhausts the mind
Home >  Coding > C# ASP.Net MVC > Sending Email via SMTP and C#

Last Updated on by

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. In this tutorial we will look at constructing an email message, adding attachments and sending email using the SMTP protocol.

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

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

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 a SMTP server and send the message.

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

That's all there is to it!

Pretty 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:

message.Body = "<p>This is my test <span style="color:blue">html</span> message.</p>";
message.IsBodyHtml = true;

SMTP Attachments

Let's send an email with an attachment, just because it's such a common task - pdf's, 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.

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.

Multiple Recipients for 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.

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.

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).

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 scalability and distribution of your web application.

<system.net>
  <mailSettings>
    <smtp>
      <network host="SERVERNAME"/>
    </smtp>
  </mailSettings>
</system.net>
SmtpClient client = new SmtpClient();
2 thoughts on “Sending Email via SMTP and C#

Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.