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 (2) all headers (like from, to, subject) (I am keeping information of those values of from, to and other properties somewhere else, so I need them too) and (3)byte[] EmailMessage.MimeContent.Content. Actually I am lacking understanding of
Microsoft.Exchange.WebServices.Data.ItemView,Microsoft.Exchange.WebServices.Data.BasePropertySetandMicrosoft.Exchange.WebServices.Data.ItemSchema
thats why I am finding it difficult.
My primary code is:
When I create PropertySet as follows:
PropertySet properties = new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent);
I get following exception:
The property MimeContent can't be used in FindItem requests.
I dont understand
(1) What these ItemSchema and BasePropertySet are
(2) And how we are supposed to use them
So I removed ItemSchema.MimeContent:
PropertySet properties = new PropertySet(BasePropertySet.FirstClassProperties);
I wrote simple following code to get all mails in inbox:
ItemView view = new ItemView(50);
view.PropertySet = properties;
FindItemsResults<Item> findResults;
List<EmailMessage> emails = new List<EmailMessage>();
do
{
findResults = service.FindItems(WellKnownFolderName.Inbox, view);
foreach (var item in findResults.Items)
{
emails.Add((EmailMessage)item);
}
Console.WriteLine("Loop");
view.Offset = 50;
}
while (findResults.MoreAvailable);
Above I kept page size of ItemView to 50, to retrieve no more than 50 mails at a time, and then offsetting it by 50 to get next 50 mails if there are any. However it goes in infinite loop and continuously prints Loop on console. So I must be understanding pagesize and offset wrong. I want to understand
(3) what pagesize, offset and offsetbasepoint in ItemView constructor means
(4) how they behave and
(5) how to use them to retrieve all mails in the inbox
I didnt found any article online nicely explaining these but just giving code samples. Will appreciate question-wise explanation despite it may turn long.