11

I create an application that gets email from mail server. I use "System.Net.Mail.MailMessage" for receive email. Now I want to get "Date and Time" of each email that ins in Inbox.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Tavousi
  • 14,848
  • 18
  • 51
  • 70
  • 3
    Be more specific if you can. DateTime from the html header? Or the date your mail client receives it? –  Oct 03 '12 at 12:51
  • I guess I want DateTime from the html header.For example in gmail, I want that DateTime that shows in each email. – Tavousi Oct 03 '12 at 13:03
  • Well I won't claim to explain anything that Micah already hasn't. Using the system.net.mail.mailmessage class you have access to all of that information you need in the Headers. Have a look at his answer and the link he's provided. –  Oct 03 '12 at 13:28

2 Answers2

20

You will want to look at the emails headers here is some documentation

http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.headers.aspx

message.Headers["Date"];
Micah Armantrout
  • 6,781
  • 4
  • 40
  • 66
  • This headers means to be just a string in rather specific format, is parsing it manually the best practice to get the date? – Johnny_D Dec 04 '12 at 12:51
  • Yes this is going to come back as a string and then you will need to use datetime.parse – Micah Armantrout Dec 05 '12 at 00:05
  • 2
    watch out - DateTime.Parse won't always be able to parse certain dates that may come back in headers : eg. "Tue, 20 Jun 2017 18:51:10 +0000 (UTC)" – Simon_Weaver Jun 21 '17 at 19:50
1

I inspected the MailMessageObject and found this:

  Headers=HeaderCollection(5) 
  {
    "Uid",
    "DateCreated",
    "DateReceived",
    "Date",
    "IsRead"

So that means you have a total of three options available to you. The output will be in string format.

message.Headers["DateCreated"];
message.Headers["DateReceived"];
message.Headers["Date"];
Mark
  • 188
  • 2
  • 8