Sending Email via SMTP and C#
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.
Adverts Blocked Please disable AdBlocking software and allow me to set cookies so that I can continue providing free content and services.
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:
So let's have a look at these and send a very simple email.
MailMessage message = new MailMessage(); message.To.Add(new MailAddress("firstname.lastname@example.org")); message.From.Add( = new MailAddress("email@example.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!
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;
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 folder\myfile.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.
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 = "firstname.lastname@example.org,email@example.com,firstname.lastname@example.org"; string toAddresses = emailList.Split(','); foreach (string address in toAddresses) message.To.Add(new MailAddress(address));
This works for the CC and BCC as well.
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.
- CC - send a carbon copy to recipients.
- BCC - send an anonymous carbon copy to recipients.
- ReplyTo - change the reply to address for the message.
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();
Last updated on: Wednesday 21st June 2017
Better than RegEx? Use this tip for email validation in C#
Calculate MD5 checksums for files and validate checksums