I\'m trying to get number of unread emails from Exchange for specific user.
I\'m able to get number of emails from Inbox like so:
SearchFilter sf = n
Here is the code for fetching the total unread count of a mailbox.
Limitations with this code snippet:
This makes only one findfolders call and work on the results.
public static int GetTotalUnreadCount(ExchangeService ewsConnector)
{
int pagedView = 1000;
FolderView fv = new FolderView(pagedView) { Traversal = Microsoft.Exchange.WebServices.Data.FolderTraversal.Deep };
fv.PropertySet = new PropertySet(BasePropertySet.IdOnly, FolderSchema.UnreadCount, FolderSchema.DisplayName);
FindFoldersResults findResults = ewsConnector.FindFolders(WellKnownFolderName.MsgFolderRoot, fv);
int totalUnreadCount = 0;
foreach (Folder f in findResults.Folders)
{
try
{
totalUnreadCount += f.UnreadCount;
Console.WriteLine("Folder [" + f.DisplayName + "] has [" + f.UnreadCount.ToString() + "] unread items.");
}
catch(Exception ex)
{
Console.WriteLine("Folder [" + f.DisplayName + "] does not have the unread property.");
}
}
Console.WriteLine("Mailbox has [" + totalUnreadCount.ToString() + "] unread items.");
return totalUnreadCount;
}