tags:

views:

57

answers:

2

Can anyone recommend a way to determine the timezone that an email was sent from in C#?

Looking at the header information of an email includes information like:

Received: from mail-ew0-f211.google.com (123.85.219.211) by UKExchange
 (10.1.10.99) with Microsoft SMTP Server id 1.2.345.6; Tue, 13 Apr 2010
 14:26:24 +0100
Received: by ewy3 with SMTP id 3so288857ewy.6        for
 <[email protected]>; Tue, 13 Apr 2010 06:26:23 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.103.213.2 with HTTP; Tue, 13 Apr 2010 06:26:23 -0700 (PDT)
X-Originating-IP: [83.244.243.210]
Date: Tue, 13 Apr 2010 14:26:23 +0100
Received: by 10.103.3.17 with SMTP id f17mr3087878mui.123.1271165183473; Tue,
13 Apr 2010 06:26:23 -0700 (PDT)

There are a number of "Received: from" and "Received: by" keys with a date, time and timezone indicated on the end.

Which one of these should I attempt to parse? Is this a reliable way to figure out the sender's timezone, or anyone recommend a better way?

+2  A: 

In theory the timestamp on the Received field is accurate. Here's the documentation:

http://cr.yp.to/immhf/date.html#timestamp

The only problem is this part:

In practice, SMTP servers put all sorts of badly formatted information into Received lines. It is probably best for readers to treat everything before the final semicolon as unstructured text, purely for human consumption.

Keltex
+2  A: 

The various "received" entries are the time each successive server have received the email in reverse order (i.e. the last server to receive the mail is on top of the stack). However, that's not the date the message was sent, it's the date the message was received by the server.

The date the message was composed is indicated by the "Date" header which should be unique.

So, depending on what you want to do exactly, you should either take the LAST "received" header (which should be the date when the message was sent according to the first SMTP server) or the "date" header (which should be the time the user pressed the "new mail" or "reply" button and contain the time zone of the client - assuming you're dealing with a real client and not web mail).

If you're dealing with web mail, then the content of the time zone could be anything really, from the real original user TZ to whatever default the web server had set.

Stephane