Requesting delivery/read receipt from C# using MailMessage class

16. October 2009 15:38

.NET Framework C#  8 Comments

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);
}
Currently rated 4.1 by 10 people

Comments (8)

Hirephpdevelopers Hirephpdevelopers
1/10/2010 6:59:27 PM #

Here given nice details about the mail messages class and also given the coding details.

Sunny Sunny
10/15/2011 12:44:09 PM #

Hi, Can we put notifications for a single email, like for Delivery and Failure both ? If Yes, then please let me know. Thanks you very much for the post above. Really helped. Thanks, Sunny.

Prashant Prashant
10/15/2011 7:01:16 PM #

The above code works for delivery notifications but I haven't tested it for failure notifications. If the above works for delivery notification then it might work for failure also. Give it a go and if it works then let me know too!!

Sunny Sunny
10/17/2011 10:20:41 AM #

Yeah, I got the point before you replied above. My point is to implement the notification for both the events, for Delivery and for Failure both. Is it possible ?

sanjiv sanjiv
11/3/2011 3:58:32 PM #

use this message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.Delay;

Dipal Dipal
6/15/2015 12:20:19 PM #

Is this code is possible for Outlook 2010 add-in??

Prashant Prashant
6/15/2015 2:49:17 PM #

@Dipal: I am not sure. I wrote it for one of the web portals, it worked fine then. The code targets the exchange server (or whatever the mail server user intended to target) and has nothing to do with Outlook. Now, as it targets the server and has nothing to do with the way Outlook do and if this is what you want to achieve then it should work for you.

Sandeep Patel Sandeep Patel
10/17/2015 2:47:59 PM #

Guys i need sql database update though button in e-mail. mail box will be outlook.I am sending a mail though c# coding and provide a approval button in mail body. I want to sql database update on click of that button.

Add Comment

Visit blogadda.com to discover Indian blogs Computers Blogs