使用s22库仅读取IMAP中的主要电子邮件

时间:2018-07-03 11:37:25

标签: c# email imap inbox

我正在使用s22 IMAP库从收件箱中读取电子邮件,效果很好,但是在读取收件箱中的所有电子邮件中,主要,社交和促销电子邮件中却只能获得主要电子邮件。

using (ImapClient client = new ImapClient("imapHost", "imapPort", true))
{
    client.Login("Email", "Password", S22.Imap.AuthMethod.Auto);
    client.DefaultMailbox = "INBOX";
    IEnumerable<uint> uids = client.Search(SearchCondition.All());
    foreach (var uid in uids)
    {
        MailMessage message = client.GetMessage(uid, false);
    }
    client.Dispose();
}

example

2 个答案:

答案 0 :(得分:1)

我不知道S22是否允许您定义自定义命令或搜索参数,或发送原始命令,但如果这样,您可以使用GMail's IMAP Extensions来使用其自定义搜索语言:

这是一个协议级别的示例,用于获取主要类别中的最新UID列表:

a UID SEARCH SINCE 1-May-2018 X-GM-RAW "Category:Primary"
* SEARCH 25032 25033 25034 25035 25036
a OK SEARCH completed (Success)

这是“促销”标签:

a UID SEARCH SINCE 1-May-2018 X-GM-RAW "Category:Promotions"
* SEARCH 25026 25028 25030 25031
a OK SEARCH completed (Success)

答案 1 :(得分:0)

如果您切换到MailKit,则可以使用MailKit的ImapFolder.Search()API进行此操作:

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

    client.Inbox.Open (FolderAccess.ReadWrite);

    foreach (var uid in client.Inbox.Search (SearchQuery.GMailRawSearch ("Category:Primary")) {
        var message = client.Inbox.GetMessage (uid);
    }

    foreach (var uid in client.Inbox.Search (SearchQuery.GMailRawSearch ("Category:Promotions")) {
        var message = client.Inbox.GetMessage (uid);
    }

    client.Disconnect (true);
}