kolexinfos
kolexinfos

Reputation: 1002

EWS Read Mail Plain Text Body Getting ServiceObjectPropertyException

I have done the following in the code below but still getting a ServiceObjectPropertyException. I am obviously loading the property as advised here too . Please can anyone help point out what I am doing wrongly

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;                   


                }   

Upvotes: 1

Views: 6018

Answers (1)

Glen Scales
Glen Scales

Reputation: 22032

With the propertyset your trying to use because you haven't used the BasepropertySet overload and you haven't added any properties your only telling exchange to return the IdOnly. So at a basic level you need to at least add the Body property eg

itemProperty.Add(ItemSchema.Body);

However you won't be able to use that propertyset in the FindItems Operations so i would suggest you change your code something like

        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);

Cheers Glen

Upvotes: 8

Related Questions