views:

481

answers:

1

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.

+2  A: 

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.

David Stratton
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 like that, 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.
Matt Refghi
I'll have to come up with a better way to do this, it seems.
Matt Refghi
+1 to the commenter for testing! There might be a way to generate or mock the mail messages for the integration component so that you don't have to actually send them through the mail server to test that portion. You could still do integration tests with the mail server in place, but these particular tests might work better as isolated unit tests.
TrueWill