0 votes
by (120 points)

I want to extract SentTime, ReceiveTime and TimeZone for both after message is loaded, how to do this ? I can see only DateTime and confused which datetime is it?

Thanks in advance.

Applies to: Rebex Time

1 Answer

0 votes
by (145k points)

1) SentTime and TimeZone

Messages in MIME format (represented in Rebex Mail by Rebex.Mail.MailMessage or Rebex.Mime.MimeMessage class) only have a Date header which contains sent time, or actually the origination date. This is what RFC 2822 says about it:

The origination date specifies the date and time at which the creator of the message indicated that the message was complete and ready to enter the mail delivery system. For instance, this might be the time that a user pushes the "send" or "submit" button in an application program. In any case, it is specifically not intended to convey the time that the message is actually transported, but rather the time at which the human or other creator of the message has put the message into its final form, ready for transport. (For example, a portable computer user who is not connected to a network might queue a message for delivery. The origination date is intended to contain the date and time that the user queued the message, not the time when the user connected to the network to send the message.

And this is also the value returned by MailMessage/MimeMessage object's Date property. This is not a DateTime, but an instance of MailDateTime which contains LocalTime, UniversalTime and TimeZone properties.

2) ReceiveTime

The only way to retrieve some equivalent of received date from a MIME message is to read it from the latest Received header. This is not done automatically by Rebex Mail yet, (partly because a huge number of mail servers don't follow the format specified by RFC 2822), but it can be retrieved quite easily:

        MailMessage mail = new MailMessage();

// display the origination date if available
        if (mail.Date != null)
            Console.WriteLine("Sent: {0}", mail.Date.LocalTime);

// find and try to parse the first 'Received' header
        MailDateTime receivedDate = null;
        string received = mail.Headers.GetRaw("Received");
        if (received != null)
            int lastSemicolon = received.LastIndexOf(';');
            if (lastSemicolon >= 0)
                string rawDate = received.Substring(lastSemicolon + 1);
                MimeHeader header = new MimeHeader("Date", rawDate);
                receivedDate = header.Value as MailDateTime;

// display the received date if available
        if (receivedDate != null)
            Console.WriteLine("Received: {0}", receivedDate.LocalTime);

Thanks for the tip, we might add something like this to one of the next releases of Rebex Mail!


This functionality is included in Release 2012 R1: MailMessage.ReceivedDate added. Extracts the date from the topmost 'Received' header.