Web Design that taps into the haromny and vision of your dreams.

Sending Email via SMTP and C#

SendMail in C# using SMTP

Written By on in C#

607 words, estimated reading time 3 minutes.

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 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;

 

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

 

Multiple Recipients

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

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

 

Comments
jeryymanly

jeryymanly

You should add network credentials to send email

SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");

SmtpServer.EnableSsl = true;

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

c# email

Reply to jeryymanly

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me