Reputation: 20291
I want to write code to get the Message body from an Exchange 2010. I am using EWS in my C# program.
I tried:
FindItemsResults<Item> fiitems = service.FindItems(folder.Id, "from:*", iv);
foreach (Item item in fiitems)
{
if (item is EmailMessage)
{
Console.WriteLine("subject");
Console.WriteLine((item as EmailMessage).Subject);
Console.WriteLine("body");
Console.WriteLine((item as EmailMessage).Body);
}
}
But I get error saying 'You must load or assign this property before you can read its value"
And then I tried:
Console.WriteLine("Subject:\t" + item.Subject);
Console.WriteLine("Title:\t" + item.TextBody);
Console.WriteLine("Received At:\t\t" + item.DateTimeReceived.ToString("dd MMMM yyyy"));
Console.WriteLine();
I get error saying 'The property TextBody is valid only for Exchange Exchange2013 or later versions" I am using Exchange2010.
Thank you for any suggestion.
Upvotes: 4
Views: 3320
Reputation: 22032
When you use the FindItems operation in EWS it will only return a subset of the properties available for an Item. One of the properties it won't return is the Body property (or any streaming property larger then 512 bytes) see http://msdn.microsoft.com/EN-US/library/office/dn600367(v=exchg.150).aspx
What you need to do is use the GetItem operation (which is the Load() method in the Managed API) to get this the most efficient way to do this is use the LoadPropertiesForItems method which will do a batch GetItem so you need to modify you code like
PropertySet Props = new PropertySet(BasePropertySet.IdOnly);
Props.Add(ItemSchema.Body);
Props.Add(ItemSchema.Subject);
FindItemsResults<Item> fiitems = null;
do
{
fiitems = service.FindItems(Folder.Id, "from:*", iv);
if (fiitems.Items.Count > 0)
{
service.LoadPropertiesForItems(fiitems.Items, Props);
foreach (Item item in fiitems)
{
if (item is EmailMessage)
{
Console.WriteLine("subject");
Console.WriteLine((item as EmailMessage).Subject);
Console.WriteLine("body");
Console.WriteLine((item as EmailMessage).Body);
}
}
}
iv.Offset += fiitems.Items.Count;
} while (fiitems.MoreAvailable);
Cheers Glen
Upvotes: 5