2

I'd like to create mirror a deep folder structure to a destination IMAP mailbox. It's illegal to create the child deep folder directly so it seems I need to create it in a manner similar to this: Add Imap Folder Mailkit

My C# is limited so I'm trying to figure out how I can make use of 'HasChildren' IMailFolder attributes to loop through all folders and maintain the reference to the parent folder when creating the folders.

Hope that makes sense!

I'm doing this which works to create the toplevel always but I don't know how to build the logic :

var toplevel = ExchangeConnection.GetFolder(ExchangeConnection.PersonalNamespaces[0]);

string foldertocreate = UserSharedBox.FullName.Replace('.', '/').Replace((string.Format("{0}/{1}", "user", sharedmailbox)), "").Replace("/","");

var CreationFolder = toplevel.Create( foldertocreate,true);

Thanks

Community
  • 1
  • 1
user219396
  • 57
  • 1
  • 7

1 Answers1

1

The easiest way to mirror the folder structure from one IMAP server to another is probably using a recursive method something like this:

public void Mirror (IMailFolder src, IMailFolder dest)
{
    // if the folder is selectable, mirror any messages that it contains
    if ((src.Attributes & (FolderAttributes.NoSelect | FolderAttributes.NonExistent)) == 0) {
        src.Open (FolderAccess.ReadOnly);

        // we fetch the flags and the internal date so that we can use these values in the APPEND command
        foreach (var item in src.Fetch (0, -1, MessageSummaryItems.Flags | MessageSummaryItems.InternalDate | MessageSummaryItems.UniqueId)) {
            var message = src.GetMessage (item.UniqueId);

            dest.Append (message, item.Flags.Value, item.InternalDate.Value); 
        }
    }

    // now mirror any subfolders that our current folder has
    foreach (var subfolder in src.GetSubfolders (false)) {
        // if the subfolder is selectable, that means it can contain messages
        var folder = dest.Create (subfolder.Name, (subfolder.Attributes & FolderAttributes.NoSelect) == 0);

        Mirror (subfolder, folder);
    }
}
jstedfast
  • 35,744
  • 5
  • 97
  • 110