Sending e-mail through code is easy, but how do we get to know that an e-mail that we send is delivered and read by the intended recipient. When we send e-mail through code we can request a delivery notification and read receipt from the receiver, similar to the way we do in outlook. But the problem is not every mail server supports this kind of request from the code. We set a mail header while sending mail from code which is then processed by the mail server and the sender of the mail gets the intended response. I have test the below code with Microsoft Exchange Server and it works perfectly fine. So let’s see what the code has:
First we need the namespace to use MailMessage class.
using System.Net; using System.Net.Mail;
Initialize MailMessage class and set To, From, Subject and Body parameters
//create the mail message MailMessage mail = new MailMessage(); //set the addresses mail.From = new MailAddress("prashantmx@xyz.com"); mail.To.Add("ankur@abc.com"); //set the content mail.Subject = "This is an email"; mail.Body = "this is the body content of the email.";
Now we have to set the Header property of the mail message class to request read/delivery recipt. As usual I first want to get notified for the e-mail I have sent has been delivered or not and then I want to get notified when my e-mail has been read by the recipient. Here I have also commented the deprecated method which is used in the previous version of .NET framework.
//This method has been deprecated. //mail.Headers.Add("Return-Receipt-To", "prashantmx@xyz.com"); //Use this if you need a delivery notification of an email. DeliveryNotificationOption is an //umeration and can be used to set the delivery notification on the following options: //1. Delay //2. Never //3. None //4. OnFailure //5. OnSuccess /You can use also use OnFailure enum with OnSuccess enum. If in case the e-mail fails to //delivered you'll get notification for both the cases mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; //Add "Disposition-Notification-To" for Read receipt mail.Headers.Add("Disposition-Notification-To", "");
When you set the headers for requesting delivery and read receipt, set the mail server and login credentials for the domain or for the e-mail service you use and then in the end call the Send method to send the e-mail.
//Set the SMTP server. You can also set the hostname instead of IP Address SmtpClient smtp = new SmtpClient("192.168.1.5"); //Set the user network/domain credentials NetworkCredential netCredit = new NetworkCredential("prashant@xyz.com", "pass#word1", "DOMAIN_NAME"); //Get SMTP to authenticate the credentials smtp.Credentials = netCredit; //Send the e-mail smtp.Send(mail);
As I mentioned earlier also I have tested the above code with Exchange server only and it works perfectly. Here is the complete listing of the code :
static void ReadReceipts() { //create the mail message MailMessage mail = new MailMessage(); //set the addresses mail.From = new MailAddress("prashantmx@xyz.com"); mail.To.Add("ankur@abc.com"); //set the content mail.Subject = "This is an email"; mail.Body = "this is the body content of the email."; //This method has been deprecated. //mail.Headers.Add("Return-Receipt-To", "prashantmx@xyz.com"); //Use this if you need an delivery notification of an email. //DeliveryNotificationOption is an enumeration //and can be used to set the delivery notification on the following options: //1. Delay //2. Never //3. None //4. OnFailure //5. OnSuccess //You can use also use OnFailure enum with OnSuccess enum. If in case the e-mail //fails to delivered you'll get notification for both the cases mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; //Add "Disposition-Notification-To" for Read receipt mail.Headers.Add("Disposition-Notification-To", ""); //Set the SMTP server. You can also set the hostname instead of IP Address SmtpClient smtp = new SmtpClient("192.168.1.5"); //Set the user network/domain credentials NetworkCredential netCredit = new NetworkCredential("prashant@xyz.com", "pass#word1", "DOMAIN_NAME"); //Get SMTP to authenticate the credentials smtp.Credentials = netCredit; //Send the e-mail smtp.Send(mail); }