How to send emails in .NET part 4: other features of the MailMessage object

We looked at the MailMessage object in various posts here, here and here. Let’s look at some less frequently used features of this object.

Notification options

You can instruct the SMTP server to send a notification to the From address depending on the status of the message. The available statuses are stored in the DeliveryNotificationOptions enumeration:

  • Never: don’t send any notification regardless of what happens with the message
  • Delay: send notification in case of a delay in the delivery
  • None: the default value, let the SMPT server decide whether to send any notification
  • OnFailure: send notification if failed
  • OnSuccess: send notification if delivery succeeded

You can also chain the options as follows:

string from = "andras.nemes@company.com";
string to = "john.smith@company.com";
string subject = "Testing notification options-";
string plainTextBody = "This is a great message.";
MailMessage mailMessage = new MailMessage(from, to, subject, plainTextBody);
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure;

Note, however, that the SMTP server may refuse to send any notification to the From address depending on its settings. So setting this property is more of a wish, unless you directly control the email server settings.

Different ReplyTo

You can change the ReplyTo address(es) of the email using the ReplyToList object of type MailMessageCollection. The effect will be that when the recipient presses Reply in her email client the To field will be populated with this modified ReplyTo address:

mailMessage.ReplyToList.Add(new MailAddress("info@company.com"));

So in case you’d like the recipient send the response to an address different from the “From” email then use this option.

Priority

Would you like to send you email with an exclamation mark so that the recipient knows that it is super-urgent? Use the Priority enumeration: High, Low or Normal, which is the default:

mailMessage.Priority = MailPriority.High;

Read all posts related to emailing in .NET here.

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to How to send emails in .NET part 4: other features of the MailMessage object

  1. Yogesh says:

    once we got delivery or read receipt…how to read those receipts from c#

Leave a comment

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.