EWS Managed API Fetch All Email Item Error

◇◆丶佛笑我妖孽 提交于 2019-12-24 02:43:34

问题


My goal is to fetch all emails in a given folder, but I keep getting the Property error:

The property Body can't be used in FindItem requests

Would you some be kind enough to point out what I am doing wrong. Below is my test code using .NET Framework 4.0

private static void GetEmailMessageCollection(ExchangeService service)
  {
     ItemView view = new ItemView(100);

     view.PropertySet = new PropertySet(PropertySet.FirstClassProperties);
     view.PropertySet.Add(ItemSchema.HasAttachments);
     view.PropertySet.Add(ItemSchema.Body);
     view.PropertySet.Add(ItemSchema.DisplayTo);
     view.PropertySet.Add(ItemSchema.IsDraft);
     view.PropertySet.Add(ItemSchema.DateTimeCreated);
     view.PropertySet.Add(ItemSchema.DateTimeReceived);

     FindItemsResults<Item> findResults;
     List<EmailMessage> emails = new List<EmailMessage>();

     string archiveFolderID = " AQEuAAADGF6AegrId0+ekrWv0TJZtgEAZ2jpm1niGUS/jwC23X6j/AAAAgP/AAAA";

     SearchFilter unreadSearchFilter = new SearchFilter.SearchFilterCollection();
     Folder boundFolder = Folder.Bind(service, archiveFolderID );
     findResults = boundFolder.FindItems(unreadSearchFilter, view);


     foreach (var item in findResults.Items)
     {
        emails.Add((EmailMessage)item);
     }

  }

Thank you.


回答1:


When you use the FindItems operation in EWS it will only return a subset of the properties available for an Item. One of the properties it won't return is the Body property (or any streaming property larger then 512 bytes) see http://msdn.microsoft.com/EN-US/library/office/dn600367(v=exchg.150).aspx

What you need to do is use the GetItem operation to get this the most efficient way to do this is use the LoadPropertiesForItems method which will do a batch GetItem so you need to modify you code like

        ItemView view = new ItemView(100);
        view.PropertySet = new PropertySet(PropertySet.IdOnly);
        PropertySet PropSet = new PropertySet();
        PropSet.Add(ItemSchema.HasAttachments);
        PropSet.Add(ItemSchema.Body);
        PropSet.Add(ItemSchema.DisplayTo);
        PropSet.Add(ItemSchema.IsDraft);
        PropSet.Add(ItemSchema.DateTimeCreated);
        PropSet.Add(ItemSchema.DateTimeReceived);

        FindItemsResults<Item> findResults;
        List<EmailMessage> emails = new List<EmailMessage>();
        do
        {
            findResults = service.FindItems(WellKnownFolderName.Inbox, view);
            if (findResults.Items.Count > 0)
            {
                service.LoadPropertiesForItems(findResults.Items, PropSet);
                foreach (var item in findResults.Items)
                {
                    Console.WriteLine(item.Body.Text);
                }
            }
            view.Offset += findResults.Items.Count;
        } while (findResults.MoreAvailable);

Cheers Glen



来源:https://stackoverflow.com/questions/25816871/ews-managed-api-fetch-all-email-item-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!