使用Mailkit将IMAP文件夹结构镜像到目标

时间:2015-11-24 12:50:20

标签: c# imap mailkit

我想将镜像深层文件夹结构创建到目标IMAP邮箱。 直接创建子深层文件夹是非法的,所以我似乎需要以类似于这样的方式创建它:Add Imap Folder Mailkit

我的C#是有限的所以我试图找出如何利用'HasChildren'IMailFolder属性来遍历所有文件夹并在创建文件夹时维护对父文件夹的引用。

希望有意义!

我这样做是为了创建顶层,但我不知道如何构建逻辑:

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);

由于

1 个答案:

答案 0 :(得分:0)

将文件夹结构从一个IMAP服务器镜像到另一个IMAP服务器的最简单方法可能是使用这样的递归方法:

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);
    }
}