This project is read-only.

How to count number of emails are there in my gmail Inbox

Topics: Issues
Jul 24, 2014 at 9:27 AM
Hi all,

I want to count the number of emails i have in my gmail inbox using IMAPX. Here is what i have written so far : -

if (client.Connect())
            Console.WriteLine("Connected Successfully.");
            if (client.Login("MyEmai, "MyPassword"))
                Folder inbox = client.Folders.Inbox;
                int count = inbox.Messages.Count();
                Console.WriteLine("Total Items:" + count.ToString());

But it is always giving 0 as an output. I m using Version 3.5 of the IMAPX 2. Kindly help me guys

Jul 24, 2014 at 2:51 PM
Hi NITIN1670,

first of all, Folder.Messages will always be empty, unless you configure the client to automatically fetch all messages, or have downloaded some already.

In order to get the number of messages in a folder, you don't have to download the messages, simply use the Folder.Exists property:
// .. create client, login

var count = client.Folders.Inbox.Exists;

// If you like to refresh the number of messages:

// Or, 
client.Folders.Inbox.Status(new[] { FolderStatusFields.Messages });

Marked as answer by NITIN1670 on 7/24/2014 at 6:58 AM
Jul 24, 2014 at 3:00 PM
thanks pavel. I have sorted out this issue by setting client.Behavior.AutoPopulateFolderMessages = true; But your answer is exactly what i was looking for. Cheers