Parsing mail header Received with multiple servers

0 votes
asked Oct 13, 2010 by Adrian (220 points)
edited Mar 12, 2012

It seems that the parser does not process the Received header correctly. It only returns the last one, but it is possible for a message to contain more than one Received header (as each server that the mail passes through adds one. I've tried with the MailMessage and MimeMessage objects and both have the same results. How can access the complete list of Received headers?

Applies to: Rebex Secure Mail

1 Answer

0 votes
answered Oct 14, 2010 by Lukas Pokorny (100,710 points)
edited Mar 12, 2012
 
Best answer

The parser processes the Received header correctly, but the name-based indexer of MimeHeaderCollection class only returns the first header value (this is documented behavior).

To access the complete list of Received headers, simply iterate through the MimeHeaderCollection. Like this, for example:

MailMessage message = new MailMessage();
...
foreach (MimeHeader header in message.Headers)
{
    if (string.Equals(header.Name, "Received", StringComparison.OrdinalIgnoreCase))
    {
        Display(header.Value);
    }
}
commented Oct 14, 2010 by Adrian (220 points)
Worked perfectly thanks! I guess in my mind I was expecting them to all be returned maybe separated with crlf.
commented Oct 15, 2010 by Lukas Pokorny (100,710 points)
Yes, I suppose we could return the Received headers separated with CRLF, but we would have to come up with another solution for other headers, and it has never really been needed. I guess adding a method called GetAllHeaderValues(string headerName) would be useful.
commented Mar 12, 2012 by Martin Vobr (12,940 points)
edited Mar 12, 2012

UPDATE: MimeHeaderCollection.GetAllHeaders and GetAllHeaderValuesRaw methods are included in Release 2012 R1 http://blog.rebex.net/news/archive/2012/03/08/version-2012-r1.aspx

...