How to send email with delivery notification in C#

In this post I am going to explain about email delivery notification in .NET. Delivery notifications used to trace the mail delivered or not and when it is delivered and is any delay in sending e-mail..!. Once mail is delivered to the recipient mailbox and delivery notification mail will be sent to the sender mailbox in case of delay or fail to delivery the mail server will send a mail back to sender.

We can set delivery notification in mail in C#.NET easily same in VB.NET. How we can do this? please check with this below code.. It is normal mail sending only. we need to and one more line to set delivery notification.

Code:-

using System.Net.Mail; //Most import System.Net.Mail
void SendUsingNetMail()
{
SmtpClient smtp = new SmtpClient();
MailMessage msg = new MailMessage(myname@domain.com,   “user@userdomain.com”);
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;      //– This is for set delivery notification mail On Success
//msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;   //– This is for set delivery notification mail On Failure
msg.Subject = “This is for Demo”;
msg.Body = “This is just for demo about delivery notification option mail option in C#“;
smtp.Send(msg);
}

In this code  it use our smtp setting to send mail, which are configured in web.config or local smtp settings (Tip:- use Web Site Administration Tool to set STMP settings).

This about code for getting delivery notification on success only. If you want to use of  on failure. use below line. If you want to sent all notifications use this below line

msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.Delay;  //this is line for set all, to sent all delivery  notifications, we need to use or(|) between them..

Please check with table more details about delivery notification options..

Member name Description
Delay Notify if the delivery is delayed.
Never A notification should not be generated under any circumstances.
None No notification information will be sent. The mail server will utilize its configured behavior to determine whether it should generate a delivery notification.
OnFailure Notify if the delivery is unsuccessful.
OnSuccess Notify if the delivery is successful.
for more details http://msdn.microsoft.com/en-us/library/system.net.mail.deliverynotificationoptions.aspx

I hope this code helpful for you..!

Enjoy while coding..!

Thanks,

Naga Harish Movva.

5 thoughts on “How to send email with delivery notification in C#

  1. We can also set like this msg.Headers.Add(“Disposition-Notification-To”, “from@domainname.com”) also. it will be useful when your using System.Web.Mail.. I hope…!

Leave a Reply