Mailkit: Sent item message id

Created on 4 Apr 2016  Â·  7Comments  Â·  Source: jstedfast/MailKit

Hi,

I don't believe this has been asked before. Is there anyway of saving the message id of a sent email message. As you know sent emails are saved if normally we connect through imap from outlook or any email client.
My scenario is that my system will send an email and if there is no reply within x number of days, it will send a reminder message with the original message attached.
Another question is when receiving an email does InReplyTo property works perfectly? As I need to check if the email received is from any email I sent.
Thanks in advance.

question

Most helpful comment

If you are sending the message over SMTP, then you have the MimeMessage locally before it is sent. All you need to do is assign it a message-id before you pass it to the SmtpClient.Send() method:

var msgid = message.MessageId = MimeUtils.GenerateMessageId ();
client.Send (message);

Then you can stick msgid into your local database or wherever you are storing it for tracking.

All 7 comments

Is there anyway of saving the message id of a sent email message.

Look at the MimeMessage.MessageId property.

Or if you are querying an IMAP server and don't want to download the full message, you can do this:

var msgid = folder.Fetch (uid, MessageSummaryItems.Envelope).FirstOrDefault ().Envelope.MessageId;

Docs for Envelope: http://www.mimekit.net/docs/html/T_MailKit_Envelope.htm

Another question is when receiving an email does InReplyTo property works perfectly?

The InReplyTo property isn't a calculation or based or heuristics or anything, it's just the parsed message-id that exists in the In-Reply-To header of the message. It's up to the client replying to the message to set that appropriately. If the client does, then it will "work perfectly". If not, then it won't.

Hope that helps.

Hi,

Thanks for the quick reply.

Sorry I am a bit confused, when I am sending a message, it goes through smtp server so how can I get the messageId of it while sending a new message?

It depends on the service. Most services don't automatically 'link' smtp and imap (gmail does, but most others I've seen don't), so your code may beed to put a copy of the message in the imap sent folder. In either case, you'll need to go to IMAP (possibly after adding the message to sent yourself) and fetch the MessageId as Jeff outlined.

Ok so is there anyway to check if smtp and imap is linked because last thing I want to do is copy to sent folder every time and have duplicates there?

Not that I know of. So far gmail has been the only one I've encountered
that automatically populates the Sent folder. All others don't.

Perhaps Jeff has a better answer.
jan

On Mon, Apr 4, 2016 at 1:55 PM, atiqi36 [email protected] wrote:

Ok so is there anyway to check if smtp and imap is linked because last
thing I want to do is copy to sent folder every time and have duplicates
there?

—
You are receiving this because you commented.
Reply to this email directly or view it on GitHub
https://github.com/jstedfast/MailKit/issues/318#issuecomment-205468761

If you are sending the message over SMTP, then you have the MimeMessage locally before it is sent. All you need to do is assign it a message-id before you pass it to the SmtpClient.Send() method:

var msgid = message.MessageId = MimeUtils.GenerateMessageId ();
client.Send (message);

Then you can stick msgid into your local database or wherever you are storing it for tracking.

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,
Capture

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

Was this page helpful?
0 / 5 - 0 ratings