Add (custom) headers to MailMessage class

Date posted: 2014-04-23
Last updated: 2026-04-12

The System.Net.Mail Namespace leaves out the required Message-ID header in email. Here is how you can add this important header to your email, to make sure email arrives.



When sending an email using the MailMessage class (System.Net.Mail namespace) in an ASP.NET website, certain email headers like Message-ID are not always set.
If a Message-ID header is missing, email might be blocked by the recipients SMTP server. Therefor it is necessary to set such headers.

Add custom headers to System.Net.Mail.

Add custom header in MailMessage class / System.Net.Mail Namespace

You set a Message-ID, or any other custom header, with the Message.Headers property and .Add method:

Message.Headers.Add("Message-Id", "<" + Guid.NewGuid().ToString() + "@example.com>");

Substitute example.com with your own domain name.

Custom headers

You can use this for other, custom headers as well, for example:

Message.Headers.Add("X-Instance-ID", Convert.ToString(InstanceID[0]));
Rate this post!

Leave a Comment


Share via
Copy link