While there is MailMessage.Headers.Add what’s the MailMessage.Headers.Add_ ?!
Add vs Add_ ?!!! Help shows they are the same!
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.
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
//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.
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:
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)
If the loaded message has “X-Unsent” header and I add this multiple times:
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:
It will add multiple “User-Agent” headers and we will have them all.