Mailkit: Question: Is it possible to move a message with IMAP from one inbox to another client inbox?

Created on 11 Feb 2020  路  2Comments  路  Source: jstedfast/MailKit

Hi there,

I am just starting with this great library. I am working on an open-source daemon that can simply move all messages from an E-Mail Account A to E-Mail Account B using IMAP.

I've been using such a tool a long day ago which was not open source and I would like to auto-move several old obsole accounts to one.

What's the best way to achieve that scenario when iterating the messages and how do I put the message to another client instance inbox?

question

All 2 comments

Probably something like:

void MoveAllMessages (IMailFolder src, IMailFolder dest)
{
    // Fetch the FLAGS and INTERNALDATE metadata for all messages in the `src`
    // folder because we'll want to clone them over to the `dest` folder when we
    // append the messages to it. We'll also want the UniqueId so that we can
    // delete the message from the `src` folder when we are done moving it over
    // to the `dest` folder.
    var items = src.Fetch (0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.InternalDate | MessageSummaryItems.Flags);
    foreach (var item in items) {
        // ignore deleted messages
        if (item.Flags.Value.HasFlag (MessageFlags.Deleted))
            continue;

        // get the message from the src folder
        var message = src.GetMessage (items.UniqueId);

        // append the message to the dest folder, keeping the flags and internal arrival date of the message as well
        dest.Append (message, item.Flags.Value, item.InternalDate.Value);

        // mark the message for deletion on the src folder
        src.AddFlags (item.UniqueId, MessageFlags.Deleted, true);
    }

    // expunge the src folder of deleted messages
    src.Expunge ();
}

Note that you don't necessarily need to or want to clone the INTERNALDATE metadata - this timestamp just represents the arrival timestamp of the message. If you don't pass that along to the destination IMAP server, then the destination IMAP server will use the current date/time as the INTERNALDATE for the message(s) that you append.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

atiqi36 picture atiqi36  路  7Comments

syneex picture syneex  路  4Comments

spchaplin picture spchaplin  路  3Comments

rgmills picture rgmills  路  6Comments

SpiritBob picture SpiritBob  路  4Comments