user3599415
user3599415

Reputation: 283

ServiceObjectPropertyException when trying to get emails with Exchange Webservices?

I am trying to get unread emails and then mark them as read.

I am getting this exception when I run the code:

ServiceObjectPropertyException was unhandeld:

An unhandled exception of type 'Microsoft.Exchange.WebServices.Data.ServiceObjectPropertyException' occurred in Microsoft.Exchange.WebServices.dll

This error occurred when I try to map my Exchange mail object to my business model object.

This is the Map method:

class MailMapper
{
     public static PhishingMail Map(EmailMessage OutlookMail)
     {
          //Map Exchange email object op Business model email object
          PhishingMail readMail = new PhishingMail();
          readMail.Subject = OutlookMail.Subject;
          return readMail;
      }
}

This is the code where it should mark emails as read.

public List<PhishingMail> GetEmails() 
{
    phishingMailList = new List<PhishingMail>();

    FolderId InboxId = new FolderId(WellKnownFolderName.Inbox, "A*******m@i*****nl");

    FindItemsResults<Item> findResults = service.FindItems(InboxId, new ItemView(100));

    foreach (Item phishingmail in findResults.Items)
    {
        ((EmailMessage)phishingmail).Load(new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.IsRead)); 

        if (!((EmailMessage)phishingmail).IsRead) 
        {
            ((EmailMessage)phishingmail).IsRead = true;
            ((EmailMessage)phishingmail)
                .Update(ConflictResolutionMode.AutoResolve);
        }

        PhishingMail mail = MailMapper.Map((EmailMessage)phishingmail);

        phishingMailList.Add(new PhishingMail());

        ///   Console.WriteLine(mail.Subject);
    }
    return phishingMailList;
}

What am I doing wrong? What is wrong with map method?

Upvotes: 4

Views: 3754

Answers (2)

Henrik H&#248;yer
Henrik H&#248;yer

Reputation: 1662

When you load an item, you speficy the properties to load.

item.Load(new PropertySet(PropertySet.FirstClassProperties)); 

If you need more properties after loading, you can do the following:

Service.LoadPropertiesForItems(items, PropertySet.FirstClassProperties);

Upvotes: 5

pjneary
pjneary

Reputation: 1246

I think the issue is that when you Load the email you're only asking for the ID and IsRead properties. Since your map routine fetches the Subject property, you would need to add that to the Load as well.

Upvotes: 1

Related Questions