How to Send Mail Using SMTP Server in C#

Kailash Chandra Behera | Tuesday, June 30, 2020

Introduction

The How to Send Mail Using SMTP Server in C#, provides code example to send email in C#. This code example you can use in any application (Console, WinForm, WPF, WCF, Web API, MVC, etc.) which supports C# language.

Here we will discuss the classes and function is used to send an email and will see how to generate an HTML page at runtime for the email body and will also discuss how to set up an SMTP server in IIS.

How to Send Mail Using SMTP Server in C#

send email using iis smtp server c#

How to Send Mail Using SMTP Server in C#

Getting Started

Before starting a demonstration for how to configure SMTP and sending the email I would like to give little idea about the SMTP and the classes in C# used to send an email. See in the below.

What is SMTP?

SMTP is the short form of The Simple Mail Transfer Protocol. It is a Communication Protocol and Internet Standard for an electronic email transformation. SMTP was introduced in 1982 and updated in 2008 to extend SMTP additions. The mail server uses it to send an electronic email.

How to Send Mail Using SMTP Server in C#

Setup Email Server Windows

The below steps guide to configure the SMTP server to send mail.

  1. Open Internet Information Services (IIS) Manager, and then navigate to the level you want to manage.

  2. Features View, double-click SMTP E-mail.

  3. On the SMTP E-mail page, type the e-mail address of the sender in the E-mail address text box.

  4. On the SMTP E-mail page, select one of the following delivery methods:

    1. Deliver e-mail to SMTP server: to deliver e-mail messages immediately. This requires an operational SMTP server for which the user has credentials.

    2. Store e-mail in pickup directory: to store e-mails in a file location on disk for later delivery.

  5. If Deliver e-mail to SMTP server is selected, configure the following:

    1. Type the unique name of your SMTP server in the SMTP Server text box, or select the Use localhost check box to set the name to LocalHost. Setting the name to LocalHost means that ASP.NET uses an SMTP server on the local computer. Typically, this is the default SMTP virtual server.

    2. Enter a TCP port in the Port text box. Port 25 is the SMTP standard TCP port and is the default setting. More than one virtual server can use the same TCP port if all servers are configured by using different IP addresses.

    3. Under Authentication Settings, specify the authentication mode and credentials if your SMTP server requires these.

  6. If Store e-mail in pickup directory is selected, type the batch e-mail location in the Store e-mail in pickup directory text box.

  7. Click Apply in the Actions pane.

How to Send Mail Using SMTP Server in C#

Demonstration

Here we will see how to send mail from console application using the SmtpClient class and an HTML page generation for the email body at run time. Before that, I would like to give you little idea about the classes.

This demonstration uses the SmtpClient class, MailMessage class, and MailAddress class. The MailMessage class represents an email message that can be sent using the SmtpClient class and its constructor takes two objects of the MailAddress class as the sender address and receiver address.

The MailAddress class represents the address of an electronic mail sender or recipient. In the constructor of the MailAddress class, an email address and display name of the user are provided. SmtpClient sends and receives emails.

The SmtpClient is used to send an email by using the Simple Mail Transfer Protocol (SMTP). It comes under the System.Net.Mail namespace and System.Net.Mail.dll. The Send method of the SmptClient class is used to trigger a mail. To construct and send an email message by using SmtpClient, you must specify the following information:

  1. Specify the name of the SMTP Server
  2. Provide specific credentials to SmptServer
  3. Create an e-mail message.
  4. Send the e-mail message

How to Send Mail Using SMTP Server in C#

Code Example How to Send Mail Using SMTP Server in C#.
 //created object of SmtpClient details and provides server details  
       SmtpClient MyServer = new SmtpClient();  
       MyServer.Host = "hostname";  
       MyServer.Port = 81;//specify your port number  
       //Server Credentials  
       NetworkCredential NC = new NetworkCredential();  
       NC.UserName = "";  
       NC.Password = "";  
       //assigned credetial details to server  
       MyServer.Credentials = NC;  
       //create sender address  
       MailAddress from = new MailAddress("Sender Address", "Name want to display");  
       //create receiver address  
       MailAddress receiver = new MailAddress("Reciver Address", "Name want to display");  
       MailMessage Mymessage = new MailMessage(from, receiver);  
       Mymessage.Subject = "This is a test mail";  
       Mymessage.Body = this.GenerateMailBody();  
       Mymessage.IsBodyHtml = true;  
       //sends the email  
       MyServer.Send(Mymessage);  

How to Send Mail Using SMTP Server in C#

Note:-The above code example sends an email as an HTLM body, if you don't want to send email as an HTML body then set the IsBodyHtml to false.

The GenerateMailBody used in the above code example to generate mail content HTML content for email body. In this method, I have used the StringBuilder class to generate HTML content.

  public string GenerateMailBody()  
     {  
       StringBuilder stringBuilder = new StringBuilder();  
       stringBuilder.Append("<HTML>");  
       stringBuilder.Append("<HEAD><TITLE>Send Email Example</TITLE><HEAD>");  
       stringBuilder.Append("<BODY>");  
       stringBuilder.Append("Hi Kailash,</br>");  
       stringBuilder.Append("Kindly find the student's report from below table.</br></br>");  
       stringBuilder.Append("<TABLE border='1px solid black;' style='border - collapse:collapse;' WIDTH='300'>");  
       stringBuilder.Append("<TR><CAPTION><B>Student's Report</B></CAPTION></TR>");  
       stringBuilder.Append("<TR bgcolor='NAVY'>");  
       stringBuilder.Append("<TH ><FONT color='white'>Roll No.</FONT></TH>");  
       stringBuilder.Append("<TH><FONT color='white'>First Name</FONT></TH>");  
       stringBuilder.Append("<TH><FONT color='white'>Class</FONT></TH>");  
       stringBuilder.Append("</TR>");  
       stringBuilder.Append("<TBODY>");  
       stringBuilder.Append("<TR><TD >0001</TD><TD>Student-1</TD><TD>VII</TD></TR>");  
       stringBuilder.Append("<TR><TD>0002</TD><TD>Student-2</TD><TD>VII</TD></TR>");  
       stringBuilder.Append("<TR><TD>0003</TD><TD>Student-3</TD><TD>VII</TD></TR>");  
       stringBuilder.Append("<TR><TD>0004</TD><TD>Student-3</TD><TD>VII</TD></TR>");  
       stringBuilder.Append("<TR><TD>0005</TD><TD>Student-3</TD><TD>VII</TD></TR>");  
       stringBuilder.Append("</TBODY>");  
       stringBuilder.Append("</TABLE></br>");  
       stringBuilder.Append("<B> Note:-</B> This is system generated email, do not reply.");  
       stringBuilder.Append("</BODy><HTML>");  
       return stringBuilder.ToString();  
     }  

How to Send Mail Using SMTP Server in C#

Summary

In blog How to Send Mail Using SMTP Server in C#, we saw how to send mail and how to use the SmtpClass, the StringBuilder class to generate an HTML string and the configuration of SMTP Server. I hope you enjoyed it a lot.

Thanks