Search code examples
c#exchange-serverexchangewebservices

EWS: Search in email body only responses 250 results


I currently got a problem with the exchange web service in C#. I'm trying to loop through mails that are older than 3 months, but I only get 250 mails back. In the folder are more than 80.000 mails so that isn't the correct count.

After searching for the problem I now know that the service only gives me 250 results per page. But the TotalCount is 250 too, so I can't work with paging because there is no second page. Without the body filter it works and I get back like 70.000 mails which is correct. Has anyone had the same problem and can help me?

Currently the programm just uses 2 filters with the SearchFilter And operator and orders the result ascending. Here is my current code for getting the mails:

Folder folder; // folder from loop
DateTime dateStart; // = today - 3 months

SearchFilter.SearchFilterCollection filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And);
filter.Add(new SearchFilter.IsLessThanOrEqualTo(EmailMessageSchema.DateTimeReceived, dateStart));
filter.Add(new SearchFilter.ContainsSubstring(ItemSchema.Body, "test"));

ItemView view = new ItemView(1000, 0, OffsetBasePoint.Beginning);
// Get oldest first
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.DateTimeReceived);
view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
view.Traversal = ItemTraversal.Shallow;

FindItemsResults<Item> findResults = exchangeService.FindItems(folder.Id, filter, view);

findResults.TotalCount() // = 250

I would be glad about a solution, thanks for helping :)


Solution

  • Am sure you have figured this out by now, but it is due to EWS throttling. While this can be changed, it is not advised. Only workaround I have found is to move the items to another folder and keep re-running logic. Here is a link to info on EWS throttling and also a way to determine your current policy - https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/ews-throttling-in-exchange.