We would like to be able to obtain unread mails from all the mailboxes contained in our exchange server. From there, we will process them in our application and then delete them. The number of unread mails will always be in small proportion.
Our first idea was to create a master mailbox, give him full access (with EPS) to other mailboxes and obtain the unread mails with EWS. We tried with this :
//Define the service
ExchangeService service = new ExchangeService(SERVICE_VERSION);
service.Url = new Uri(SERVICE_URL);
service.Credentials = new WebCredentials("MyAdmin", "MyPassword");
//define our search
FolderView viewFolders = new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep, PropertySet = new PropertySet(BasePropertySet.IdOnly) };
ItemView viewEmails = new ItemView(int.MaxValue) { PropertySet = new PropertySet(BasePropertySet.IdOnly) };
SearchFilter unreadFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
//search all items in Inbox and subfolders
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Root, unreadFilter, viewEmails);
//bind the result
ServiceResponseCollection<GetItemResponse> responseList =
service.BindToItems(findResults.Select(item => item.Id), new PropertySet(BasePropertySet.FirstClassProperties,EmailMessageSchema.ReceivedBy, EmailMessageSchema.From, EmailMessageSchema.Subject, EmailMessageSchema.Attachments));
However this will only get the unread mails from the MyAdmin mailbox. With further investigation, we found this way to obtain unread mail from another specific mailbox :
//Define the service
ExchangeService service = new ExchangeService(SERVICE_VERSION);
service.Url = new Uri(SERVICE_URL);
service.Credentials = new WebCredentials("MyAdmin", "MyPassword");
//define our search
FolderView viewFolders = new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep, PropertySet = new PropertySet(BasePropertySet.IdOnly) };
ItemView viewEmails = new ItemView(int.MaxValue) { PropertySet = new PropertySet(BasePropertySet.IdOnly) };
SearchFilter unreadFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
//define the user mailbox
var userMailbox = new Mailbox("[email protected]");
var folderId = new FolderId(WellKnownFolderName.Inbox, userMailbox);
//search all items in Inbox
var userItems = service.FindItems(folderId, unreadFilter, viewEmails);
The problem with this solution is for 3000 mailboxes, we must then query 3000 times the EWS to obtain the unread mails from every mailbox (Knowing that many won't have unread mails).
Is there a way to obtain, in a single call, all the unread mails from every mailboxes of the exchange server? We are using exchange 2013 with the latest EWS.
Thanks in advance