0

到目前为止,我一直在使用MailSystem.Net 的这段代码$"SENTSINCE {Date}"从 Imap INBOX 获取电子邮件,并添加了使用.

string mailBox = "INBOX";
public IEnumerable<Message> GetMailsSince(string mailBox) {
  return GetMails(mailBox, $"SENTSINCE {DateTime.Now.AddDays(-3).ToString("dd-MMM-yyyy")}").Cast<Message>();
}

private MessageCollection GetMails(string mailBox, string searchPhrase) {
  Mailbox mails = Client.SelectMailbox(mailBox);
  MessageCollection messages = mails.SearchParse(searchPhrase);
  return messages;
}

但即使在研究了几个小时的 mailkit 之后,我似乎也无法提炼出如何做同样的事情。我的目标是获取消息对象的列表,然后我可以将其属性映射到我创建的另一个类,该类将其写入 mysql 数据库。我还想将附件保存到磁盘。到目前为止,所有这些都运行良好,但性能是一个问题。我希望 mailkit 会大大改善这一点。

我的主要来源是这里的示例,但因为我还不熟悉异步编程和树视图,所以很难看穿它。

如何将我想要的“INBOX”硬编码为“IMailFolder”?我可以在哪里或如何在 Mailkit 中使用“SENTSINCE {Date}”过滤器?如何获得与邮件系统中的 Message 对象等效的 Mailkits 的“IEnumerable”(可能是“IMessageSummary”)?

如果您可以向我指出一些代码,或者甚至将链接的 MailSystem.Net 示例转换为 Mailkit,那就太棒了。

4

2 回答 2

3

MimeMessage相当于 MailSystem.NET 的Message对象,但这不是您想要的。您想要的是 MailKit IMessageSummary,它允许您下载单个 MIME 部分(也称为“附件”)。

它还允许您非常快速地获取有关消息的摘要信息(标志、接收日期(又名“InternalDate”)预解析/解码的常见标头值(例如主题、发件人、收件人等),因为 IMAP 服务器具有这些部分缓存在其数据库中以便快速检索的信息。

using (var client = new ImapClient ()) {
    client.Connect ("imap.mail-server.com", 993, SecureSocketOptions.SslOnConnect);
    client.Authenticate ("username", "password");

    // if you don't care about modifying message flags or deleting
    // messages, you can open the INBOX in read-only mode...
    client.Inbox.Open (FolderAccess.ReadOnly);

    // search for messages sent since a particular date
    var uids = client.Inbox.Search (SearchQuery.SentAfter (date));

    // using the uids of the matching messages, fetch the BODYSTRUCTUREs
    // of each message so that we can figure out which MIME parts to
    // download individually.
    foreach (var item in client.Inbox.Fetch (uids, MessageSummaryItems.BodyStructure MessageSummaryItems.UniqueId)) {
        foreach (var attachment in item.Attachments.OfType<BodyPartBasic> ()) {
            var part = (MimePart) client.Inbox.GetBodyPart (item.UniqueId, attachment);

            using (var stream = File.Create (part.FileName))
                part.ContentObject.DecodeTo (stream);
        }
    }
}

注意:每个属性IMessageSummary都有一个相应的MessageSummaryItems枚举值,您需要使用它来填充该属性。

例如,如果您想使用IMessageSummary.Envelope,则需要MessageSummaryItems.Envelope在您的Fetch()请求中包含。

由于MessageSummaryItems[Flags]属性标记,您可以像这样一起按位或枚举值:

MessageSummaryItems.BodyStructure | MessageSummaryItems.Envelope并且将获取两条信息。

更新:

这是更接近 MailSystem.NET 的低效方式。

using (var client = new ImapClient ()) {
    client.Connect ("imap.mail-server.com", 993, SecureSocketOptions.SslOnConnect);
    client.Authenticate ("username", "password");

    // if you don't care about modifying message flags or deleting
    // messages, you can open the INBOX in read-only mode...
    client.Inbox.Open (FolderAccess.ReadOnly);

    // search for messages sent since a particular date
    var uids = client.Inbox.Search (SearchQuery.SentAfter (date));

    // using the uids of the matching messages, fetch the BODYSTRUCTUREs
    // of each message so that we can figure out which MIME parts to
    // download individually.
    foreach (var uid in uids) {
        var message = client.Inbox.GetMessage (uid);

        foreach (var attachment in message.Attachments.OfType<MimePart> ()) {
            using (var stream = File.Create (attachment.FileName))
                attachment.ContentObject.DecodeTo (stream);
        }
    }
}

注意:如果您关心保存消息/rfc822 附件,请查看此 StackOverflow 答案:MailKit 保存附件

于 2017-10-30T15:33:41.290 回答
1

“收件箱”文件夹在 IMAP 邮件帐户中始终可用。使用 MailKit,它可以作为ImapClient.Inbox. 对于日期过滤,您可以使用DateSearchQuery该类。MailKit 的入门页面几乎涵盖了您所有的问题。

于 2017-10-30T08:27:11.730 回答