7

Using the SmtpClient and MailMessage classes in .NET to send emails through a local mail server (hMailServer), I currently found no way to get the Message-ID header value of a sent message.

The idea behind

I'm trying to programmatically track messages that are undeliverable, so I have to find a way to identify replies from the destination SMTP server that rejects a certain message.

Now I thought of simply remembering the Message-ID SMTP header value and parse incoming mails for this ID.

I've tried to inspect the Headers collection after sending the message, but I did not find any Message-ID.

My question

Is it possible to get the Message-ID header value that my SMTP server adds during sending of a MailMessage instance?

Update 2012-05-27

As per this example I've successfully tried to manually generate a Message-ID on my own, just before sending.

All my examples work so far, so it seems that this is a solution to my question.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 2
    I'm pretty sure the id is assigned by the SMTP server, and the SMTP protocol does not specify returning that value back to the client. A way around this would be to BCC the same message to an account you control, and extract the message id from that message... assuming that the message id is generated once, at the initial SMTP server. Another possibility is to add a custom "X-tag" to the email. – Jeremy Holovacs May 27 '12 at 19:24
  • Thanks, @JeremyHolovacs - I've already tried to add a custom tag (not with "X-" prefix, though) it seems that this tag is lost when forwarding or replying to the message. – Uwe Keim May 27 '12 at 19:26
  • 1
    Unfortunately per http://stackoverflow.com/questions/7095104/how-to-confirm-that-mail-has-been-delivered-or-not there's not a bulletproof method for this. – Jeremy Holovacs May 27 '12 at 19:41
  • 1
    Jeremy Holovacs: That is not the problem here. For bounce tracking, you know the message was not delivered, and you want to find out for which address the bounce was generated. It's hard, but doable. Also for the record, most MSAs in practice allow you to specify your own Message-Id; but, as noted in my answer, that doesn't really help, because some bounces stupidly omit the original Message-Id. – tripleee May 27 '12 at 20:39
  • 1
    @tripleee, I'm not sure that's true in all circumstances, i.e., I do not believe a true mail relay is obligated by RFC-defined protocol to relay a failure notification. I admit I could be wrong about this though. – Jeremy Holovacs May 29 '12 at 20:37
  • 1
    The absence of a bounce falls outside the scope of this discussion as I understand it anyway. – tripleee May 29 '12 at 21:02

3 Answers3

5

You can add your own message id before send the email. I use the next code:

Guid id = Guid.NewGuid(); //Save the id in your database 
mensajeEmail.Headers.Add("Message-Id", String.Format("<{0}@{1}>",id.ToString(),"mail.example.com"));

Note: For download messages I use OpenPop.Net, I check the message.Headers.InReplyTo property, and there is the message id sended.

Juan Carlos Velez
  • 2,840
  • 2
  • 34
  • 48
2

The standard solution to your problem is VERP. Read Bernstein's original article to find out why Message-Id et al. are not reliable. http://cr.yp.to/proto/verp.txt

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    Trying to figure out how to do VERP with GMail... Can't send using plus addresses since GMail overrides with the email on the authenticated account (even when your plus address is based on the authenticated email address). – MikeJansen Sep 27 '12 at 16:26
  • 1
    @MikeJansen: If you have a question, post a question. – tripleee Sep 28 '12 at 08:06
  • 1
    sorry, not sure why I posted that comment. Can't really do VERP with gmail from what I've found. This post summarizes what I've found: http://stackoverflow.com/questions/9925392/does-gmail-not-allow-sender-to-set-a-return-path-value-to-receive-bounce-message. I'll need to use another SMTP server, my own or a another provider. – MikeJansen Sep 28 '12 at 12:19
2

I'm Using MailKit library for .Net and SMTP Client.

I tried another solution to get the ID of the message sent with SMTP Client, to trace any replied messages.

Before you send your message add a hidden ID property to the message headers,

Check this sample

Now continue and send your message, Wait about 10 Seconds, Then you will use the IMAP Client to get your Sent folder and for each message in your folder, loop over the message headers and check if anyone of them is ==messageIdentity, now you catch your sent message successfully and get any information about it you wand like ID etc...

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291