Gembox.Email.Imap get the count of unread mails

414 Views Asked by At

i have been searching all over the place for a way to get the count of unread emails in my Gmail account using GemBox.Email.Imap. So far i am able to connect, get the count of all mails but i need just the unread, is there anyone that have some experience using this Package?.

2

There are 2 best solutions below

0
On

First you need an IMAP client; this is the client you get from the library:

private readonly MailKit.Net.Imap.ImapClient _client = new MailKit.Net.Imap.ImapClient();

Next, open access to your inbox folder:

await _client.Inbox.OpenAsync(FolderAccess.ReadOnly).ConfigureAwait(false);

Finally, get the list of messages with the flag "Not seen" and after return the count of values:

var result = _client.Inbox.Search(SearchQuery.NotSeen);

return result?.Count;
0
On

Okay, after a little bit i found out how to make this work, this is the code for a simple console aplication but it is extensible for any case.

using System;
using System.Collections.Generic;
using GemBox.Email;
using GemBox.Email.Imap;

namespace IMapAccess
{
    class Program
    {
        static void Main(string[] args)
        {
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");

            using (ImapClient imap = new ImapClient("imap.gmail.com", 993)){
                imap.ConnectTimeout = TimeSpan.FromSeconds(10);
                imap.Connect();
                imap.Authenticate("[email protected]", "MySuperSecretPassword", ImapAuthentication.Native);
                imap.SelectInbox();
                IList<int> messages = imap.SearchMessageNumbers("UNSEEN");
                Console.WriteLine($"Number of unseen messages {messages.Count}");
            }
        }
    }
}