0 votes
by (420 points)

The MailMessage class has two properties that hold datetime info:

  • Date
  • ReceivedDate

First one is the value set by the mail client that sent the mail and the other one is set by the mail server that received the mail, correct? I found out that the first one can be null, is it possible that the other as well? Should I also worry about null in case of From, To, CC properties?

I would like to create a hash for each mail header that identifies mail copies among many mailboxes and in order to construct that hash I use: From, To, CC and Date properties. I can't use message id since Outlook sends mail with one id and saves copy of the mail in Sent folder with different message id. I want to detect email duplicates based only on email headers.

Applies to: Rebex Secure Mail
by (420 points)

Update: I can't use ReceivedDate in hash function since the same mail received by two different mail server can have different ReceivedDate values and i will not detect this duplication. So if Date can be null then there is no good way of detecting duplicates...?

1 Answer

0 votes
by (58.9k points)
Best answer


yes, you are right, the ReceivedDate is added into the Received header by the receiving server. Technically it is possible for the ReceivedDate property of the MailMessage class to be null. For a real email received by a server, that would be in the case when the date in the Received headers is in a strange format which we would not be able to parse.

In fact I was able to prepare a message which has no ReceivedDate and no senders and recipients at all (in Outlook I created a draft new empty email and saved it as a MSG file).

The email after loading into MailMessage has From, To, CC and Bcc collections all empty, and at the same time ReceivedDate is null. Also the Sender is null.

If you consider that it is possible to have more empty mails like that in the Draft folder which could have different mail bodies, I think it is not a good idea to detect duplicates by comparing email headers only.

You can load the message and see yourself with the following code:

var mail = new MailMessage();

Console.WriteLine("Sender is null: " + (mail.Sender == null));
Console.WriteLine("#To: " + mail.To.Count);
Console.WriteLine("#From: " + mail.From.Count);
Console.WriteLine("#CC: " + mail.CC.Count);
Console.WriteLine("#BCC: " + mail.Bcc.Count);
Console.WriteLine("Date: " + mail.Date);
Console.WriteLine("ReceivedDate is null: " + (mail.ReceivedDate == null));