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

C# EWS读取邮件纯文本正文获取ServiceObjectPropertyException,c#,api,exchangewebservices,C#,Api,Exchangewebservices,我在下面的代码中完成了以下操作,但仍然得到了ServiceObjectPropertyException。很明显,我正在按建议装货。谁能帮我指出我做错了什么 this.ExchangeService = new ExchangeService(ExchangeVersion.Exchange2013); this.ExchangeService.Credentials = new WebCredentials(mailBox, password);

我在下面的代码中完成了以下操作,但仍然得到了ServiceObjectPropertyException。很明显,我正在按建议装货。谁能帮我指出我做错了什么

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;                   


                }   
this.ExchangeService=新的ExchangeService(ExchangeVersion.Exchange2013);
this.ExchangeService.Credentials=新的WebCredentials(邮箱、密码);
this.ExchangeService.Url=新Uri(“https://mail.xxxxxxxxxxx.com/EWS/Exchange.asmx");                
PropertySet itemProperty=新的PropertySet();
itemProperty.RequestedBodyType=BodyType.Text;
SearchFilter SearchFilter=new SearchFilter.SearchFilterCollection(LogicalOperator.And,new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead,false));
ItemView视图=新的ItemView(999);
view.PropertySet=itemProperty;
列表电子邮件=新列表();
FindItemsResults emailMessage=this.ExchangeService.FindItems(WellKnownFolderName.Inbox、searchFilter、view);
foreach(emailMessage中的邮件)
{
ExchangeMailResponse电子邮件=新的ExchangeMailResponse();
mail.Load(itemProperty);
email.Message=mail.Body.Text;
}   

使用您试图使用的属性集,因为您没有使用BasepropertySet重载,并且您没有添加任何属性,您只能告诉exchange仅返回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);
SearchFilter SearchFilter=newsearchfilter.SearchFilterCollection(LogicalOperator.And,newsearchfilter.IsEqualTo(EmailMessageSchema.IsRead,false));
PropertySet FindItemPropertySet=新的PropertySet(BasePropertySet.IdOnly);
ItemView视图=新的ItemView(999);
view.PropertySet=FindItemPropertySet;
PropertySet GetItemsPropertySet=新的PropertySet(BasePropertySet.FirstClassProperties);
GetItemsPropertySet.RequestedBodyType=BodyType.Text;
FindItemsResults emailMessages=null;
做
{
emailMessages=service.FindItems(WellKnownFolderName.Inbox、searchFilter、view);
如果(emailMessages.Items.Count>0)
{
服务.LoadPropertiesForItems(emailMessages.Items、GetItemsPropertySet);
foreach(emailMessages.Items中的项目)
{
Console.WriteLine(Item.Body.Text);
}
}
}while(emailMessages.moreavable);
干杯
Glen

与您尝试使用的propertyset一起使用,因为您没有使用BasepropertySet重载,并且您没有添加任何属性,这是您唯一告诉exchange仅返回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);
SearchFilter SearchFilter=newsearchfilter.SearchFilterCollection(LogicalOperator.And,newsearchfilter.IsEqualTo(EmailMessageSchema.IsRead,false));
PropertySet FindItemPropertySet=新的PropertySet(BasePropertySet.IdOnly);
ItemView视图=新的ItemView(999);
view.PropertySet=FindItemPropertySet;
PropertySet GetItemsPropertySet=新的PropertySet(BasePropertySet.FirstClassProperties);
GetItemsPropertySet.RequestedBodyType=BodyType.Text;
FindItemsResults emailMessages=null;
做
{
emailMessages=service.FindItems(WellKnownFolderName.Inbox、searchFilter、view);
如果(emailMessages.Items.Count>0)
{
服务.LoadPropertiesForItems(emailMessages.Items、GetItemsPropertySet);
foreach(emailMessages.Items中的项目)
{
Console.WriteLine(Item.Body.Text);
}
}
}while(emailMessages.moreavable);
干杯 峡谷