2

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?.

Mario Z
  • 4,328
  • 2
  • 24
  • 38
AcidRod75
  • 187
  • 9

2 Answers2

2

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("MyEmail@gmail.com", "MySuperSecretPassword", ImapAuthentication.Native);
                imap.SelectInbox();
                IList<int> messages = imap.SearchMessageNumbers("UNSEEN");
                Console.WriteLine($"Number of unseen messages {messages.Count}");
            }
        }
    }
}
AcidRod75
  • 187
  • 9
0

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;
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77