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

10,523

Solution 1

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

Solution 2

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.

Solution 3

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...

Share:
10,523
Uwe Keim
Author by

Uwe Keim

German developer. Some of my apps: SharePoint Systemhaus Göppingen (zwischen Stuttgart und Ulm) Eigene Homepage erstellen Test Management Software Windows 10 Ereignisanzeige Very proud father of Felix (2012) and Ina (2014). Loves running, climbing and Indian food. Code Project member #234.

Updated on June 28, 2022

Comments

  • Uwe Keim
    Uwe Keim almost 2 years

    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.