Is it possible to capture the "Message-ID" of an email message sent with SmtpClient?
Asked Answered
P

3

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.

Polypody answered 27/5, 2012 at 19:18 Comment(6)
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.Lilas
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.Polypody
Unfortunately per #7095604 there's not a bulletproof method for this.Lilas
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.Wallflower
@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.Lilas
The absence of a bounce falls outside the scope of this discussion as I understand it anyway.Wallflower
W
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

Wallflower answered 27/5, 2012 at 20:35 Comment(3)
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).Gimp
@MikeJansen: If you have a question, post a question.Wallflower
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: #9925892. I'll need to use another SMTP server, my own or a another provider.Gimp
V
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.

Velleman answered 3/7, 2015 at 16:26 Comment(1)
OpenPop.Net is valid now ?Cusick
W
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

Wallflower answered 27/5, 2012 at 20:35 Comment(3)
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).Gimp
@MikeJansen: If you have a question, post a question.Wallflower
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: #9925892. I'll need to use another SMTP server, my own or a another provider.Gimp
P
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...

Probability answered 10/7, 2019 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.