MimeKit: How to delete an email by MessageId?
Asked Answered
T

2

8

I am using MailKit/MimeKit 1.2.7 (latest NuGet version).

Deleting an email with the ImapClient is pretty easy ...

client.Inbox.AddFlags(uniqueId, MessageFlags.Deleted, silent: true);

... if you know the emails UniqueId or its Index.

In my case, I don't know either one nor the other. All I have is the message itself (MimeMessage) ad its MessageId.

I was hoping that MessageId == UniqueId, but obviously this is not the case.

Do I have any chance to delete an email by just having the corresponding MimeMessage/MessageId?

Tomokotomorrow answered 15/7, 2015 at 9:0 Comment(0)
B
12

You could try doing something like this:

var uids = folder.Search (SearchQuery.HeaderContains ("Message-Id", message.MessageId));
folder.AddFlags (uids, MessageFlags.Deleted, silent: true);

Ideally, though, you'd keep track of the UniqueId that you used to fetch the message so that you can just use that value.

Bluebell answered 15/7, 2015 at 16:0 Comment(2)
What is a unique ID and where do I get it? I can retrieve messages without ever seeing a unique ID, so there's no "keeping track" of it. The index seems like a very dangerous thing because messages could come and go while I'm iterating the folder. I'd just like to delete a message, like message.Delete() or folder.Delete(message).Henning
UIDs can be gotten from a folder.Search() query or from a folder.Fetch() if you request the MessageSummaryItems.UniqueId. This is just how the protocol works.Bluebell
S
0

In addition to the folder.AddFlags (uids, MessageFlags.Deleted, silent: true) add

folder.Expunge ()

complete code may look like with attachment download feature

using (var client = new ImapClient ())  {
    client.Connect ("imap.friends.com", 993, true);

    client.Authenticate ("joey", "password");

    for (int i = 0; i < client.Inbox.Count; i++) {
    var message = client.Inbox.GetMessage (i);
    Console.WriteLine ("Subject: {0}", message.Subject);

    //Downlaod attachement in MimeKit c#

    var firstFrom = message.From.FirstOrDefault() as MimeKit.MailboxAddress;
                   
    log.Info("Message from: " + firstFrom?.Name+ "<" +firstFrom?.Address+">");

    var dataFilePath= @"C:\tools\mailprocessing\Inbox\";

        foreach (var attachment in message.Attachments)
        {
            string filePath = Path.Combine(dataFilePath, attachment.ContentDisposition.FileName);
            log.Info("Save to path: " + filePath);
            using (var stream = File.Create(filePath))
            {
                if (attachment is MimeKit.MessagePart)
                {
                    var part = (MimeKit.MessagePart)attachment;

                    part.Message.WriteTo(stream);
                }
                else
                {
                    var part = (MimeKit.MimePart)attachment;

                    part.Content.DecodeTo(stream);
                }
            }

            log.Info("Downloaded file: " + attachment.ContentDisposition.FileName);
        }

        //Mark mail flag as deleted
        var uids = client.Inbox.Search (SearchQuery.HeaderContains ("Message-Id", message.MessageId));
        client.Inbox.AddFlags (uids, MessageFlags.Deleted, silent: true);
    }
    client.Inbox.Expunge();
    client.Disconnect (true);
}
Sedulous answered 14/5 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.