【问题标题】:Efficient FETCH query and parsing results高效的 FETCH 查询和解析结果
【发布时间】:2014-03-06 19:40:25
【问题描述】:

我打算使用 MailSystem.NET 来阅读所有邮件(我猜是草稿中的东西除外)并提取 UUID、日期、发件人、所有收件人电子邮件。

我想让它从最近的开始并继续向后扫描,一次加载可能 100 个标头的批次。

如果在此操作期间有新邮件进来,我不希望邮件索引更改影响进度。

我想对此提出一些建议。在我看来,Fetch() 包装器适用于单个消息,并且收件箱搜索功能提供消息索引序号而不是 UID。我相信 UID 对并发活动会更加健壮。

我可以调用 imap.Command("fetch 1:100 (uid rfc822.header)") 但是我不知道如何使用 MailSystem.NET 来解析结果。

另外,有没有办法说“获取 UID 小于上次看到的 UID 的下 100 条消息”?如果可以安全地假设 UID 总是随着后面的消息而增加。我的意思是这是基于消息索引序数的替代方法。

谢谢!

【问题讨论】:

    标签: c# imap mailsystem.net


    【解决方案1】:

    我无法使用 MailSystem.NET 回答您的问题,但我可以使用更好的 C# IMAP 库来回答这个问题:MailKit

    using System;
    
    using MailKit.Net.Imap;
    using MailKit;
    using MimeKit;
    
    namespace TestClient {
        class Program
        {
            public static void Main (string[] args)
            {
                using (var client = new ImapClient ()) {
                    client.Connect ("imap.friends.com", 993, true);
    
                    // Note: since we don't have an OAuth2 token, disable
                    // the XOAUTH2 authentication mechanism.
                    client.AuthenticationMechanisms.Remove ("XOAUTH");
    
                    client.Authenticate ("joey", "password");
    
                    // The Inbox folder is always available on all IMAP servers...
                    client.Inbox.Open (FolderAccess.ReadOnly);
    
                    // Note: The envelope contains the date and all of the email addresses
                    var items = MessageSummaryItems.UniqueId | MessageSummaryItems.Envelope;
                    int upper = client.Inbox.Count - 1;
                    int lower = Math.Max (upper - 100, 0);
    
                    while (lower <= upper) {
                        foreach (var message in client.Inbox.Fetch (lower, upper, items)) {
                            Console.WriteLine ("Sender: {0}", message.Envelope.Sender);
                            Console.WriteLine ("From: {0}", message.Envelope.From);
                            Console.WriteLine ("To: {0}", message.Envelope.To);
                            Console.WriteLine ("Cc: {0}", message.Envelope.Cc);
                            if (message.Envelope.Date.HasValue)
                                Console.WriteLine ("Date: {0}", message.Envelope.Date.Value);
                        }
    
                        upper = lower - 1;
                        lower = Math.Max (upper - 100, 0);
                    }
    
                    client.Disconnect (true);
                }
            }
        }
    }
    

    【讨论】:

    • 对不起,是的,我是想输入 993
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2015-04-19
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 2013-05-11
    相关资源
    最近更新 更多