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.

Advertisements

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: