Requesting delivery/read receipt from C# using MailMessage class

16. October 2009 15:38

.NET Framework C# 

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.2 by 11 people

T-SQL: Recently Executed Query

14. October 2009 05:16

SQL Server T-SQL 

I am a regular reader of Pinal Dave's blog SqlAuthority. I always found something new in his blog to work with SQL Server. Here is something I would like to share: Get the recent executed SQL Queries from SQL Server.

SELECT deqs.last_execution_time AS [Time], dest.TEXT AS [Query]
FROM sys.dm_exec_query_stats AS deqs CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle)
AS destORDER BY deqs.last_execution_time DESC
 No Rating

.NET Framework 4.0 Poster

29. September 2009 15:30

.NET Framework Silverlight 

The new .NET Framework 4.0 poster in deepzoom.

Download PDF: PDC2008-NETFX4.zip (3.22 mb)

Currently rated 4.8 by 5 people

Microsoft Natal - Welcome To The Next Level Of Gaming

27. September 2009 15:06

Microsoft 

Microsoft promised that they are going to provide user a next level of gaming experience. Take a look of what Microsoft has planned for all the gamers out there......This is a jaw-dropping technologySurprised

[flv:Microsoft_Natal.flv]

 No Rating

Microsoft Event - Windows Mobile 6.5 & .NET Framework 4.0

26. September 2009 22:24

Microsoft 

I attended the Microsoft developers event at NCUI auditorium in New Delhi on Windows Mobile® 6.5 and .NET Framework 4.0. Abhishek Baxi was a key speaker on Windows Mobile® 6.5. You can read his blog here. He demonstrates all the new features that Win Mob provides to the end users. How do we sync our data using cloud (Windows Live Mesh), configuring Outlook (Exchange ID) and even configure your live and other IDs, access internet using IE and develops gadgets and other application using VS2008 and 2010 on your phone. He also tells us about Microsoft Marketplace and how do we developers get benefits from the marketplace.

There is lot more to tell about Microsoft Windows Mobile® 6.5, but recently I bought Sony Ericsson phone but now after having a close look on Windows Mobile® I feel really bad. My phone don't have those features....that's not good...but it's too late now. I can't do anything about it...but I am not going to stuck with this phone foreverCool

Resources for Windows Mobile® 6.5:

After this we have a half-an-hour break time and then we get back in to attend Bijoy's Singhal's .NET Framework's session. Bijoy is a Developer Evengelist at Microsoft. You can read his blog here. So, he started up to make all the developer's aware of the new features of .NET Framework 4.0 provides. He continues to talk about parallel computing and parallel computing, PLINQ, DLR, enhancements in CLR, MVC, MEF, MAF and other tons of features...which is totally outstanding.

In the end we move to the round-table session where we all developers start with a short introduction of ourselves and end up with a feedback on Microsoft Technologies / Events / Cons / Pros and how can we make more and more people aware of the fact on how Microsoft Technologies affect their lives. Moving further we also discussed on how do we get more and more people especially students at the events so they know about the new technology and eventually this will help them as a fresher to choose a domain / technology to move on. Even personally I have seen students that they are very confused about on what they have to choose .....the technology they want to work in. They just think should I choose Java or .NET but they find it hard to decide. So it's better that someone from Microsoft or on behalf of Microsoft can guide them to choose what's best for them. All other developers also put questions regarding Microsoft certifications and the black market associated with it. Bijoy guarantees that he will be informing Microsoft regarding the same and will definitely take a serious action against this. After all this is about software giant's reputation.

Finally, we decided to get connected once again and we will make our best efforts to promote Microsoft Technologies at every level as we can. I myself is a DieHard Microsoft Fan and I personally will make every possible effort to make people / students aware / beleive in the power of technology Microsoft provides.

 No Rating