Question:
I’d like to add some custom headers to an email, then read them back in if I receive a bounce email.
I add a custom header to the email I send like this:
email.AddHeaderField ("XYZ-messageCode", "testcode")In the program that iterates the bounced emails I have this:
str = email.GetHeaderField(("“XYZ-MessageCode")
However the string is Nothing. I have changed the priority to high via the header. If I iterate through the headers, none are the ones I’ve added via AddHeaderField.
You are expecting something that cannot be expected -- that the bounced email will also contain the same header (i.e. the email that comes back if the server decides to send a non-delivery notification of some sort). You have no control over what a remote mail server decides to send for a "bounce notification", or if it even decides to send anything at all. It is most likely that your custom headers will not be part of any NDR (non-delivery report) you receive, unless the mail server decides to send an entire copy of the original email in a sub-part of the NDR. In that case, you would have to identify it and parse it. Remember: there is no single "format" for bounced email. Mail servers and other programs send all sorts of ad-hoc non-delivery notifications..
I should also say that there are formats defined for NDR's (or MDN's) as shown here: https://www.example-code.com/csharp/emailObject_createMdn.asp
Also as shown here: https://tools.ietf.org/html/rfc3798
However, that does not mean a server will send a bounce notification in this format. It's possible, but it cannot be guaranteed, and there's no guarantee you'll get anything at all.