Search code examples
c#imapmimekit

How to filter emails with Imap from a specific sender in C#?


Instead of getting the number 1 - because there is only one sender that has sent a single email to my inbox with the specified email, instead I get the number 19.

using(ImapClient client = new ImapClient())
            {
                client.Connect("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);
                client.Authenticate("a.t@gmail.com", "pass");
                var inbox = client.Inbox;
                inbox.Open(FolderAccess.ReadOnly);
                var count = inbox.Search(SearchQuery.CcContains("b.c@gmail.com"));

                Console.WriteLine(count);
            }

Solution

  • As @Fildor mentioned in a comment Search() returns the list of unique ids. Therefore try

    var count = inbox.Search(SearchQuery.CcContains("b.c@gmail.com")).Count;