Get the number of emails in the mailbox


Below is the code to get the emails in the mailbox using VSTO Outlook.

  // Create Application class and get namespace

 Outlook.Application outlook = new Outlook.Application();

 Outlook.NameSpace ns = outlook.GetNamespace("Mapi");

 object _missing = Type.Missing;

 ns.Logon(_missing, _missing, false, true);

 // Get Inbox information in objec of type MAPIFolder

 Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

 int i = inbox.Items.Count;

 MessageBox.Show("Message count: " + i);


Below is the code to get the emails in the mailbox using for .NET.

  string MailBoxURI = "http://MachineName/exchange/Username";

 string UserName = "username";

 string Password = "password";

 string Domain = "domain";

 // Create instance of ExchangeClient class by giving credentials

 ExchangeClient client = new ExchangeClient(MailBoxURI,UserName, Password, Domain);

 // Call ListMessages method to list messages info from Inbox

 ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.MailboxInfo.InboxUri);

 int i = msgCollection.Count;

 Console.WriteLine("Message count: " + i);

Download Source Code

Download Running Example