c# - EWS 读取邮件纯文本正文获取 ServiceObjectPropertyException

标签 c# api exchangewebservices

我在下面的代码中完成了以下操作,但仍然收到 ServiceObjectPropertyException。我显然正在按照建议加载属性 here too .请任何人帮助指出我做错了什么

this.ExchangeService = new ExchangeService(ExchangeVersion.Exchange2013);

                this.ExchangeService.Credentials = new WebCredentials(mailBox, password);
                this.ExchangeService.Url = new Uri("https://mail.xxxxxxxxxxx.com/EWS/Exchange.asmx");                

                PropertySet itemProperty = new PropertySet();
                itemProperty.RequestedBodyType = BodyType.Text;


                SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));

                ItemView view = new ItemView(999);
                view.PropertySet = itemProperty;                

                List<ExchangeMailResponse> emails = new List<ExchangeMailResponse>();

                FindItemsResults<Item> emailMessage = this.ExchangeService.FindItems(WellKnownFolderName.Inbox, searchFilter, view);                               


                foreach (Item mail in emailMessage)
                {

                    ExchangeMailResponse email = new ExchangeMailResponse();


                    mail.Load(itemProperty);                  

                   email.Message = mail.Body.Text;                   


                }   

最佳答案

使用您尝试使用的属性集,因为您没有使用 BasepropertySet 重载并且您没有添加任何属性,您唯一告诉交换返回 IdOnly。因此,在基本层面上,您至少需要添加 Body 属性,例如

itemProperty.Add(ItemSchema.Body);

但是您将无法在 FindItems 操作中使用该属性集,因此我建议您更改代码,例如

        SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));

        PropertySet FindItemPropertySet = new PropertySet(BasePropertySet.IdOnly);

        ItemView view = new ItemView(999);
        view.PropertySet = FindItemPropertySet;
        PropertySet GetItemsPropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
        GetItemsPropertySet.RequestedBodyType = BodyType.Text;


        FindItemsResults<Item> emailMessages = null;
        do
        {
            emailMessages = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);
            if (emailMessages.Items.Count > 0)
            {
                service.LoadPropertiesForItems(emailMessages.Items, GetItemsPropertySet);
                foreach (Item Item in emailMessages.Items)
                {
                    Console.WriteLine(Item.Body.Text);
                }
            }
        } while (emailMessages.MoreAvailable);

干杯 格伦

关于c# - EWS 读取邮件纯文本正文获取 ServiceObjectPropertyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36069801/

相关文章:

C# 扩展 where 方法 - 处理未找到元素的情况

c# - NHibernate 中的 Future() 是什么意思?

c# - 使用csc时出现奇怪的警告

c# - 在 C# 中读取 Excel 范围内的所有单元格值

javascript - 如何通过 javascript 使谷歌地图地理编码器超时?

delphi - Delphi 2010 的 QT 绑定(bind)

java - 使用 Java API 获取 Elasticsearch 版本

c# - 使用 EWS 托管 API 维护拉取通知中的亲和性

c# - GetRoomLists 成功但没有返回数据

.net - 从 Exchange 检索电子邮件,按收到的日期时间排序