I want to fetch all mails in the Inbox folder using EWS Managed API and store them as .eml
. The problem is in fetching (1) all mails with
EWS is a bit inconsistent with the properties returned from various operations. An Item.Bind will not return the exact same properties as a FindItem. You're using PropertySets properly as far as defining what you want from the server, but you have to use them in the right place. What you need to do is find the items, then load the properties into them. It's not ideal, but that's the way EWS works. With your loop, you're constantly assigning 50 to your offset when you need to increment it by 50. Off the top of my head, something like this would do:
int offset = 0;
int pageSize = 50;
bool more = true;
ItemView view = new ItemView(pageSize, offset, OffsetBasePoint.Beginning);
view.PropertySet = PropertySet.IdOnly;
FindItemsResults<Item> findResults;
List<EmailMessage> emails = new List<EmailMessage>();
while(more){
findResults = service.FindItems(WellKnownFolderName.Inbox, view);
foreach (var item in findResults.Items){
emails.Add((EmailMessage)item);
}
more = findResults.MoreAvailable;
if (more){
view.Offset += pageSize;
}
}
PropertySet properties = (BasePropertySet.FirstClassProperties); //A PropertySet with the explicit properties you want goes here
service.LoadPropertiesForItems(emails, properties);
Now you have all of the items with all of the properties that you requested. FindItems often doesn't return all of the properties you want even if you ask for them, so loading only the Id initially and then loading up the properties you want is generally the way to go. You may also want to batch the loading of properties in some way depending on how many emails you're retrieving, perhaps in the loop prior to adding them to the List of EmailMessages. You could also consider other methods of getting items, such as a service.SyncFolder action.