Insert header at top


I saw (Creating and Setting Contents of Emails in .NET|Documentation) that I can insert a header at the end of the collection by Add method.

What if I want to insert at the beginning of the collection?



Hi Roi,

Thank you for contacting Apose support team.

Headers.Insert can be used to insert particular header at the top or whichever position you like. This information is available at the end of article you mentioned above. Please give a try to the following sample code and share the feedback with us.

//Create an instance MailMessage class
MailMessage msg = new MailMessage();
msg.Headers.Insert("Test-Header", "Test Header value");

//Specify ReplyTo

//From field
msg.From = "";

//To field

//Adding Cc and Bcc Addresses

//Message subject
msg.Subject = "test mail";

//Specify Date
msg.Date = new System.DateTime(2016, 5, 10);

//Specify XMailer
msg.XMailer = "Aspose.Email";
msg.Body = "Sample body";

//Save message to disc
msg.Save(@"MsgHeaders.eml", EmlSaveOptions.DefaultEml);


It didn’t work for me. This is the code:

string emlPath = “goodmail.eml”;
using (MailMessage mail = MailMessage.Load(emlPath))
mail.Headers.Insert(“X-Roi”, "");
mail.Save(emlPath + “after.eml”);

I’m using Aspose.Email.dll version

Please your help,



Hi Roi,

In the above sample mail was generated from scratch, that is why the header was placed on top. In your case, you are loading an existing mail, therefore, it appends header at the end of existing collection. I am afraid that no such option is available which can be used to insert header at the top of an existing headers collection.

Should you have any other query related to Aspose.Email, please feel free to write us back.