Share via


Adding a "Message-Id" header to an email created using C#

Question

Saturday, July 14, 2018 8:15 AM

Google recommend that emails sent to @gmail.com addresses include a valid Message-Id in the header - otherwise Google may treat the email as spam.  And observation suggests the latter is indeed true.

How do I add a Message-Id line to the email header, when creating the email using C# (or outlook.application)?

All replies (5)

Saturday, July 14, 2018 1:42 PM âś…Answered

If you are using System.Net.Mail to send your messages, then you can access the collection of headers by means of the property Headers of the MailMessage object.

/en-us/dotnet/api/system.net.mail.mailmessage.headers?view=netframework-4.7.1#System_Net_Mail_MailMessage_Headers

Simply do .Add on the collection to add any headers that you want.


Saturday, July 14, 2018 7:57 PM

Thank you for that.  I had kind of assumed that the Headers property was read-only, but you have now pointed out that that is not so.  I'll follow this up further.


Saturday, July 14, 2018 9:17 PM

Yes, the Headers property itself is read-only... but it returns a NameValueCollection, and this one is not read-only -- you can add entries to the collection.


Sunday, July 15, 2018 12:24 AM

Here is another resource http://www.systemnetmail.com/faq/3.3.4.aspx

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator


Sunday, July 22, 2018 4:09 PM

Alberto

Yes that did work - in a C# console test program.  However, my application is written in Visual FoxPro and currently uses Outlook via COM to send the email. So I tried to create a C# DLL that could be called from VFP, in place of using Outlook COM, but failed to get the two to work together.

Nevertheless, I also send emails from a C# Windows service, and your input will be useful there.