Fetching all mails in Inbox from Exchange Web Services Managed API and storing them as a .eml files RRS feed

  • Question

  • I want to fetch all mails in the Inbox folder using EWS Managed API and store them as .eml. I can store file once I get the file content as a byte[] will not be difficult, as I can do:


    The problem will be to fetch (1) all mails with (2) all headers (like from, to, subject) (I am keeping information of those values of fromto 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.BasePropertySet and
    • Microsoft.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

    (Q1) What these ItemSchema and BasePropertySet are

    (Q2) 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>();
        findResults = service.FindItems(WellKnownFolderName.Inbox, view);
        foreach (var item in findResults.Items)
        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

    (Q3) what pagesizeoffset and offsetbasepoint in ItemView constructor means

    (Q4) how they behave and

    (Q5) 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.

    • Edited by Mahesha999 Wednesday, December 18, 2013 4:09 PM
    Wednesday, December 18, 2013 4:08 PM


  • 1) With FindItems it will only return a subset of Item properties see for a list and explanation. To get the mime content you need to use a GetItem (or Load) I would suggest you read which also covers of paging as well.

    3) offset is from the base your setting the offset to 50 each time which means your only going to get the 50 items from the offset of 50 which just creates an infinite loop. You should use

    view.Offset = +50;

    to increment the Offset although it safer to use

    view.Offset  += findResults.Items.Count;

    which increments the offset based on the result of the last FindItems operation.

    5) try something like

                ItemView iv = new ItemView(100, 0);
                FindItemsResults<Item> firesults = null;
                PropertySet psPropSet = new PropertySet(BasePropertySet.IdOnly);
                iv.PropertySet = psPropSet;
                PropertySet itItemPropSet = new PropertySet(BasePropertySet.IdOnly) { ItemSchema.MimeContent, ItemSchema.Subject, EmailMessageSchema.From };
                    firesults = service.FindItems(WellKnownFolderName.Inbox, iv);               
                    service.LoadPropertiesForItems(firesults.Items, itItemPropSet);
                    foreach(Item itItem in firesults){                    
                        Object MimeContent = null;
                        if(itItem.TryGetProperty(ItemSchema.MimeContent,out MimeContent)){
                            Console.WriteLine("Processing : " + itItem.Subject);
                    iv.Offset += firesults.Items.Count;
                } while (firesults.MoreAvailable);

    .Offset += fiFitems.Items.Count;

    • Marked as answer by Mahesha999 Thursday, December 19, 2013 10:53 AM
    Thursday, December 19, 2013 5:27 AM