+1 vote

I have seen the example to move email between folders. Is it possible to copy and or move email between two different imap accounts?

by

1 Answer

0 votes

Unfortunately there is no such feature in IMAP protocol. Please note that this is the limitation of the protocol not Mail.dll.

You can however download a message from one account and upload it to another. You don't even need to parse it.

Below you can find a simple code that downloads a raw message from one account and uploads it to another. Of course you can have both Imap instances opened in the same time:

var eml;

using (Imap imap = new Imap())
{
    imap.Connect("imap.example1.com");    // use ConnectSSL for SSL connection.
    imap.UseBestLogin("user", "password");

    imap.SelectInbox();

    List<long> uids = imap.GetAll();
    long first = uids[0];

    eml = imap.GetMessageByUID(first);

    imap.Close();
}

using (Imap imap = new Imap())
{
    imap.Connect("imap.example2.com");    // use ConnectSSL for SSL connection.
    imap.UseBestLogin("user", "password");

    imap.SelectInbox();

    imap.UploadMessage(eml);

    imap.Close();
}
by (301k points)
Thanks, this works great. Just one more questions.
I still want to retain all original info such as message received timestamp, attachments etc. How do I do that ?
Attachments are part of the email you download. All email headers, attachments, visual element, email headers, date, form, to information are retained.

The only things that are not retained are: message IMAP flags (such as \Seen, \Recent) and internal IMAP server date (date assigned to the message by the IMAP server).

There is an UploadMessage overload that takes UploadMessageInfo class instance. You can use its Flags property and SetInternalDate method to specify those 2 properties.

To get those values you'll need to use GetMessageInfo() method:
https://www.limilabs.com/blog/get-email-information-from-imap-fast
Thank you, this works great. however, after doing the migration from gmail to yahoo mail, couple questions popped up.

1. The guid from gmail doesn't really seem to be retained at yahoo (I guess this is expected). Is there any other way to search whether a message has previously been migrated? This is for failure cases where retry needs to be done.
2. Copy all folders and messages blindly seems to create the same effect as that of labels in gmail but I'm not sure if these are marked as duplicates or the label info gets carried over. Deleting any of the messages from the target seems to delete all the messages from other folders as well.
Those are 2 different email systems, and Gmail in many ways is not behaving as regular IMAP servers.

Labels are Gmail's-only feature. Gmail stores a single version of an email (in "All Mail"), even if the message is available through different folders (labels). You can operate using "All Mail" folder only and use gmail label extensions: https://www.limilabs.com/blog/label-message-with-gmail-label

If you upload a message to Yahoo to two different folders - it will be duplicated. Two different versions of the message will be stored on the server.

You can use message-id to identify messages or remember message UID (and folder) assigned by the server on upload (it is returned by the UploadMessage method, if the  server supports UIDPLUS extension)

To learn more about uids go here: https://www.limilabs.com/blog/unique-id-in-imap-protocol
...