• Home
  • About
Blue Orange Green Pink Purple

Emails in C#: Delivery and Read receipts / Attachments

Posted in code. on Wednesday, January 13th, 2010 by Chris Hulbert
Jan 13

When sending emails with System.Net.Mail in C#, you can specify you want certain receipts:

Delivery receipts: These are the receipt emails that get sent back to you by *your* email server when it is satisfied that the email was sent to the other's server. This has no bearing on whether the other person actually saw the email, but on the other hand it doesn't require them to allow a receipt. You can get these this way:

msg.DeliveryNotificationOptions =
  DeliveryNotificationOptions.OnFailure |
  DeliveryNotificationOptions.OnSuccess |
  DeliveryNotificationOptions.Delay;

Read receipts: These are the emails that arrive in your inbox, saying something like 'the sender of this email requested a receipt upon reading it. Shall i send it?'. The receipt is optional and it requires their email client to support it, but it's a better way of gauging whether the person at the other end actually got it. Hint - i use this way, not the delivery receipts. And here's some code (change the email address!):

msg.Headers.Add("Disposition-Notification-To", "chris@splinter.com.au");

And here's a complete guide to emails:

using System.Net.Mail;
...

MailMessage msg = new MailMessage();

msg.From = new MailAddress("me@wherever.com","My name");

msg.To.Add("recipient1@abcdef.com"); // Who's it to
msg.To.Add("recipient2@abcdef.com");
msg.CC.Add("recipient3@abcdef.com"); // CC recipient(s)
msg.CC.Add("recipient4@abcdef.com");

msg.Subject = "Subject line";
msg.Body = "<html><h1>Big header</h1><p>And some smaller text</p></html>"
msg.IsBodyHtml = msg.Body.Contains("<html>");

// Delivery notifications
msg.DeliveryNotificationOptions =
  DeliveryNotificationOptions.OnFailure |
  DeliveryNotificationOptions.OnSuccess |
  DeliveryNotificationOptions.Delay;

// Ask for a read receipt
msg.Headers.Add("Disposition-Notification-To", "chris@splinter.com.au");

// Attachment(s)
byte[] data = File.ReadAllBytes("attachment.dat");
msg.Attachments.Add(new Attachment(new MemoryStream(data),"MyAttachment.dat"));

// Send it
new SmtpClient("my-smtp-server").Send(msg);

3 Comments

  1. Mina Samy on March 5th, 2010

    Hi Chris
    I believe this code won't work if the sender's mail is different from that specified in the
    Disposition-Notification-To
    so is there another way to provide a mail different from that of the sender's
    thank

  2. Chris Hulbert on March 5th, 2010

    You're probably right mina, it'd likely not work if the addresses were different - i guess this is an anti-spam / phishing feature of most email programs.

  3. Metin ACAR on March 31st, 2010

    Thank you so much !



Leave a Reply

Chris' Babble

  • About
    Hi, i'm Chris Hulbert, a software guy from Sydney Australia.
  • Categories
    • code
    • Portfolio
    • Uncategorized
  • Recent Articles
    • How to implement DES and Triple DES from scratch
    • How to use Cookies in Struts 2 with ServletRequest and ServletResponse
    • How to use sessions with Struts 2
    • Using Quartz Scheduler in a Java web app (servlet)
    • Javascript date picker that Doesn't Suck!(tm)
    • Using Oracle XE with Hibernate


  • Home
  • About

© Copyright Chris' Babble. All rights reserved.
Designed by FTL Wordpress Themes brought to you by Smashing Magazine

Back to Top