exchange-serverexchangewebservicesmanaged-ews

FindItems() and BindToItems() give inconsistent results for EmailMessage.Sender.Address


After quite a lot of debugging, I've refined a complicated Managed EWS problem down to the following two simple-ish test cases. The first one works, the second one fails:

var view = new ItemView(100) { PropertySet = new PropertySet { EmailMessageSchema.Id } };
var findResults = ews.FindItems(WellKnownFolderName.Inbox, view)
var bindResults = ews.BindToItems(findResults.Select(r => r.Id), new PropertySet { EmailMessageSchema.Sender });

// Sanity check
Assert.AreEqual(1, bindResults.Count());

// The results I care about
Assert.AreEqual("David Seiler", bindResults[0].Sender.Name);
Assert.AreEqual("david.seiler@yahoo.com", bindResults[0].Sender.Address);

One might try to cut out the BindToItems() call, and use FindItems() directly:

var view = new ItemView(100) { PropertySet = new PropertySet { EmailMessageSchema.Sender } };
var findResults = ews.FindItems(WellKnownFolderName.Inbox, view)

// This part still works fine
Assert.AreEqual(1, findResults.Count());

// So does this
Assert.AreEqual("David Seiler", findResults[0].Sender.Name);

// ...but this fails!  Sender.Address is null
Assert.AreEqual("david.seiler@yahoo.com", findResults[0].Sender.Address);

Can anyone tell me where I've gone wrong? It really seems, from the documentation, as though this should work. Not all properties can be read through FindItems(), it's true, but those properties usually throw when I try to access them, and anyway there's a list of those properties on MSDN and Sender isn't on it. What's going on?


Solution

  • Actually I don't know why, but in the second option, it only load basic information of the sender like the name, but not the Address.

    If you want to load all the sender properties but do not want to bind the full message you can add the following line before the first assert

    service.LoadPropertiesForItems(findResults.Items, new PropertySet(EmailMessageSchema.Sender));