Question

I tried to accomplish this via a MailMessage.Headers.Set call, in VB.net. See below:

    Dim objMail As MailMessage
    Dim objSMTPClient As SmtpClient

    objMail = New MailMessage()
    objSMTPClient = New SmtpClient()

    objMail.From = New MailAddress(MY_EMAIL_ADDRESS)
    objMail.To.Add(New MailAddress(TEST_EMAIL_ADDRESS))
    objMail.Headers.Set("Date", "09 Jan 1999 17:23:42 -0400")'date in the past'

    objMail.Subject = "The Subject"
    objMail.Body = "The Body"

    objSMTPClient.Port = 25
    objSMTPClient.Host = HOST_IP
    objSMTPClient.Credentials = New System.Net.NetworkCredential(MY_EMAIL_ADDRESS, txtPassword.Text)
    objSMTPClient.Send(objMail)

I confirmed that the objMail.Headers.Set call is actually working - if I get the value afterwards, it has been successfully changed. My problem is that when I receive the e-mail in the TEST_EMAIL_ADDRESS's Outlook, the date is 2009 everywhere, not 1999. Right there in the Outlook interface, and also in the header, which I access via the "Options" item in the context menu for that e-mail.

What am I doing wrong? I have a feeling I missed something obvious...

To be clear: I am not doing this with malicious intent. I am working on an e-mail integration component that utilizes both UIDs and a "Last processed" date to locate the first new e-mail to integrate. I want to test cases where multiple e-mails have the exact same date/time - as the e-mail integration module should handle those situations flawlessly. If I could simply fake the date this way, I could send as many e-mails as I wanted that matches a certain date/time, rather than trying to send them with an automated script - hoping they will all be received within the same second. It seems I'll need to take another approach, though.

Was it helpful?

Solution

Adding on to @Lasse V. Karlsen's note, I think that most likely the SMTP server is overriding what you put into the message in code. In all honesty, why would you even need to make an email look like it was sent in the past? I can't think of a single reason that isn't dishonest at least or malicious at worst.

So, it would make sense to me that the server would overwrite this header if it looked suspicious. I don't know if that's what's actually happening, but I would bet that @Lasse V. Karlsen is correct.

If so, a possible solution would be to change the date on the SMTP server to some date in the past (if you control the server and are able to do so).

Still, I am wondering why you would want to even do this. Can you elaborate?

Added

@Lasse V. Karlsen - I think you should post your note as the answer so you get the credit for it.

OTHER TIPS

The header value is being overwritten in the Message class when preparing headers:

this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Date), MailBnfHelper.GetDateTimeString(DateTime.Now, null));

The reason that I'm trying (unsuccessfully) set this value is because I need to know it for a different calculated header. I guess "close enough" will have to do.

You can add custom headers to the Header property; however, not to few reserved headers including Date. The documentation states that custom value to restricted headers will be discarded or overwritten read more here on MSDN

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top