Insert header at top

Hi,


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?

Thanks,

Roi

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
msg.ReplyToList.Add("reply@reply.com");

//From field
msg.From = "sender@sender.com";

//To field
msg.To.Add("receiver1@receiver.com");

//Adding Cc and Bcc Addresses
msg.CC.Add("receiver2@receiver.com");
msg.Bcc.Add("receiver3@receiver.com");

//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);

Hi,


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”, "roy@roy.com");
mail.Save(emlPath + “after.eml”);
}

I’m using Aspose.Email.dll version 6.5.0.0

Please your help,

Thanks,

Roi

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.