-3

I am using s22 IMAP library to read emails from inbox it is work fine but it in read all email in inbox Primary, social and promotion emails how I can get only primary email.

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 Answers2

1

I don't know if S22 will let you define custom commands or search parameters, or send raw commands, but if so, you can use GMail's IMAP Extensions to use their custom search language:

Here's a protocol level example of getting a list of recent UIDs in the primary category:

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

And here's the promotions tab:

a UID SEARCH SINCE 1-May-2018 X-GM-RAW "Category:Promotions"
* SEARCH 25026 25028 25030 25031
a OK SEARCH completed (Success)
Max
  • 10,701
  • 2
  • 24
  • 48
  • (Note, it's unclear to me whether 'Primary' needs to be localized per language, like the folder names) – Max Jul 03 '18 at 15:21
  • Also note: there does not appear to be any protocol way to get categories for a message, only messages for a category. – Max Jul 03 '18 at 16:28
0

If you switch to MailKit, you can do this using MailKit's 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);
}
jstedfast
  • 35,744
  • 5
  • 97
  • 110