Header collection


  1. While there is MailMessage.Headers.Add what’s the MailMessage.Headers.Add_ ?!
    Add vs Add_ ?!!! Help shows they are the same!

  2. Need to add 2 headers so using MailMessage.Headers.Add will work but can’t implement:

  • Need those 2 to be added to the very beginning of the source email.
  • Need to overwrite or replace them if exist in source message, now, each time they are added, multiple same headers will be added.

Is that possible? If so, how?

If not, can you please consider this:

Adding 2 extra parameters (overload) will be helpful:

overwrite / Boolean
If true all found headers having the specified name are removed, while only one header will be inserted instead of them; otherwise, another header having the same name will be added.

index / Int32
The zero-based index of the newly added header in the collection.

If set to 0 this header will be added to the very beginning and first line of source message, if set to 1, the second line and go on…
Thank you in advance.

thanks, we’ll look into this and share our thoughts

1 Like

Thank you for your kind consideration :slight_smile:

Hi @australian.dev.nerds

The Add method throws an ArgumentNullException when trying to add a header with name = null, the Add_ method prevents such an exception.

There are two ways to achieve this goal:
//1st way - before adding remove the header with the desired name
eml.Headers.Add(“somename”, “somevalue”);
//2nd way - use an indexer
eml.Headers[“somename”] = “somevalue”;

At the moment there is no such possibility, we will discuss this proposal and share our solution in this thread.

1 Like

Hello and thanks for your help, appreciated.

So it’s kinda TryAdd equivalent, not mentioned in the docs.

Good, can you please confirm if we go the 1st way and remove the header first:
Because we’re not sure if “somename” header exists or not, if it does not exist, Headers.Remove will not throw an exception? I hope it safely ignores with no exception.

I asked this because when trying to save as .emltpl which is basically the same as .eml, but there’s an initial line inside .emltpl like this:
User-Agent: Microsoft-MacOutlook/16.66.22100900
So to write my own agent name I need it to be at the first line (might be safe at the other lines but no official ref to check and make sure)

Thanks :slight_smile:

Strange behavior, can you please confirm?

If the loaded message has “X-Unsent” header and I add this multiple times:
MailMessage.Headers.Add(“X-Unsent”, “1”)
It will not add multiple “X-Unsent” headers and we will have only one, nice, internally replace.

But if the loaded message has “User-Agent” header and I add this multiple times:
MailMessage.Headers.Add(“User-Agent”, “Blah”)
It will add multiple “User-Agent” headers and we will have them all.

Different unspecified logic.

No exceptions will be thrown, a bool result will be returned instead.
bool isSuccess = eml.Headers.Remove(“somename”);//true if the header was found, false if not

This is normal behavior for headers that can be used once per message ( From, Content-Type, Date, etc.). For headers that can be used multiple times, they will be added multiple times (Received, Keywords, etc.).