MailKit:邮件客户端显示的是移动时收到的消息,并且不保留原始标志

时间:2018-07-20 18:27:39

标签: c# mailkit

我正在编写脚本,以将电子邮件从一个帐户迁移到另一个帐户。我有两个问题。

  1. 消息正在移动,但是邮件客户端显示了它们 收到的日期作为移动的日期/时间(在消息列表中) 视图),而不是邮件标题中显示的日期/时间。我是 猜猜是否不保留文件日期?

  2. 未复制消息标志。如留言 已被读取。我希望看到所有标记都通过了...基本上,我需要移动上一个帐户中存在的消息。

    protected void CopyBtn_Click(object sender, EventArgs e)
    {
        try
        {
            ImapClient Client = new ImapClient();
            ImapClient Client2 = new ImapClient();
            Client.Connect(SourceHostBox.Text.Trim(), 993, SecureSocketOptions.SslOnConnect);
            Client.Authenticate(SourceUsernameBox.Text.Trim(), SourcePasswordBox.Text.Trim());
            Client.Inbox.Open(FolderAccess.ReadWrite);
            Client2.Connect(DestinationHostBox.Text.Trim(), 993, SecureSocketOptions.SslOnConnect);
            Client2.Authenticate(DestinationUsernameBox.Text.Trim(), DestinationPasswordBox.Text.Trim());
            Client2.Inbox.Open(FolderAccess.ReadWrite);
    
            var folders = Client.GetFolders(Client.PersonalNamespaces[0]);
    
            //move all messages in folders & create folders if necessary
            foreach (var folder in folders)
            {
                folder.Open(FolderAccess.ReadWrite);
                var uids = folder.Search(SearchQuery.All);
    
                foreach (var uid in uids)
                {
                    var folders2 = Client2.GetFolders(Client2.PersonalNamespaces[0]);
                    var message = folder.GetMessage(uid);
                    string currentFolder = folder.ToString().Replace("INBOX.", ""); //Remove the 'INBOX.' text that's getting prepended by cPanel/Dovecot
    
                    var toplevel = Client2.GetFolder(Client2.PersonalNamespaces[0]);
                    var folderExists = FindFolder(toplevel, currentFolder);
                    if (folderExists == null)
                        toplevel.Create(currentFolder, true);
                    Client2.GetFolder(currentFolder).Append(message);
                }
            }
    
            //move inbox messages
            Client.Inbox.Open(FolderAccess.ReadWrite);
            Client2.Inbox.Open(FolderAccess.ReadWrite);
            var inboxuids = Client.Inbox.Search(SearchQuery.All);
            foreach (var uid in inboxuids)
            {
                var message = Client.Inbox.GetMessage(uid);
                Client2.Inbox.Append(message);
            }
    
            label1.Text = "Finished Successfully.";
            label1.ForeColor = System.Drawing.Color.Green;
    
        }
        catch (Exception ex)
        {
            label1.Text = ex.Message;
            label1.ForeColor = System.Drawing.Color.Red;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

您需要使用其他Append()方法之一,该方法需要一个MessageFlags参数和一个DateTimeOffset参数来指定消息到达的时间戳。

但是为了获取该信息,您还需要Fetch()的每条消息的元数据。

这是我要更改循环的方式:

var inboxuids = Client.Inbox.Search(SearchQuery.All);
foreach (var uid in inboxuids)
{
    var message = Client.Inbox.GetMessage(uid);
    Client2.Inbox.Append(message);
}

已修复:

var uids = Client.Inbox.Search (SearchQuery.All);
var items = Client.Inbox.Fetch (uids, MessageSummaryItems.InternalDate | MessageSummaryItems.Flags);
foreach (var item in items)
{
    var message = Client.Inbox.GetMessage (item.UniqueId);

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