By Mark Taylor
Where is that darn message body anyway? I have seen this question (or variations on it) many times. Someone writes code to use the Exchange Web Service (EWS) proxy objects to enumerate through all the items in a mailbox folder to get the message body content for each item. Even though the code they write retrieves the items, the message body content is nowhere to be found. The code required to get the message body content isn't as obvious as you might think. To learn the secret, read on!
The process for getting the message body is not intuitive because of how the EWS proxy objects are created. The naming conventions don't help, either. Most developers use FindItem with an AllProperties BaseShape to return all the items in the folder, then enumerate through the items returned, expecting the message body to be one of the items.
That process would work very well if FindItem with an AllProperties BaseShape actually returned the message body — but, to minimize the resource usage of FindItem, it doesn’t. AllProperties in a FindItem call does not return all the properties attached to an item. Large binary objects or text properties such as the Body, and calculated properties (those whose value depends on other properties), are not returned in a FindItem call.
Actually, the AllProperties BodyShape in a GetItem call doesn’t return all the properties set on an item, either. It returns all the properties that we have mapped in our schema. Believe me — we’re beginning to regret our choice of naming there!
But don’t worry, there is a way to retrieve the message body — it just involves a few extra steps. See the code example that follows for the inside scoop!
You can also retrieve the body summary in a GetItem call. An interesting post (Accessing Web service URL) on the Exchange Server Development TechNet Forum discussed this.
Properties Returned Only by GetItem
The following table lists the properties that are returned by both GetItem and FindItem.
Properties Returned by Both FindItem and GetItem
Many times you'll want to enumerate through mailbox items and retrieve the Body, and you'll also want the attachments. To do this, you call FindItem to get the list of items, and then call GetItem to retrieve the item and its properties, including the Body. To get the attachment content, you call GetAttachment and pass in the AttachmentId.
The following code shows you how to retrieve any attachments on the item, by using the following process:
1. Calling FindItem on the mailbox folder to retrieve the list of ItemIds for the items.
2. Calling GetItem on each mailbox item to retrieve the Body property and AttachmentIds for the item.
3. If the mailbox item has any attachments, calling GetAttachment to retrieve each one.
// First, set up the binding to Exchange Web Services.
ExchangeServiceBinding binding = new ExchangeServiceBinding();
binding.Credentials = new NetworkCredential("fred", "Pa$$word", "contoso");
binding.Url = @"https://exchange.contoso.com/EWS/Exchange.asmx";
// Next, call FindItem for the Inbox folder.
// Form the FindItem request.
FindItemType findItemRequest = new FindItemType();
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
// Define which item properties are returned in the response.
// We only need the ItemIds.
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
itemProperties.BaseShape = DefaultShapeNamesType.IdOnly;
// Add the properties shape to request.
findItemRequest.ItemShape = itemProperties;
// Identify which folders to search to find items.
DistinguishedFolderIdType folderIDArray = new DistinguishedFolderIdType;
folderIDArray = new DistinguishedFolderIdType();
folderIDArray.Id = DistinguishedFolderIdNameType.inbox;
// Add folders to the request.
findItemRequest.ParentFolderIds = folderIDArray;
// Send the request and get the response.
FindItemResponseType findItemResponse = binding.FindItem(findItemRequest);
// Next, enumerate through the Items returned by FindItem.
foreach (FindItemResponseMessageType responseMessage in findItemResponse.ResponseMessages.Items)
if (responseMessage.ResponseClass == ResponseClassType.Success)
ArrayOfRealItemsType mailboxItems =
for (int itemCount = 0; itemCount <
ItemType inboxItem = mailboxItems.Items[itemCount];
// Call GetItem on each ItemId to retrieve the
// item’s Body property and any AttachmentIds.
// Form the GetItem request.
GetItemType getItemRequest = new GetItemType();
// AllProperties on a GetItem request WILL return
// the message body.
getItemRequest.ItemIds = new ItemIdType;
GetItemResponseType getItemResponse =
// We only passed in one ItemId to the GetItem
// request. Therefore, we can assume that
// we got at most one Item back.
ItemInfoResponseMessageType getItemResponseMessage =
if (getItemResponseMessage != null)
if (getItemResponseMessage.ResponseClass ==
// Now the message Body is there.
BodyType messageBody = inboxItem.Body;
// You now have the mailbox item's Body property
// and you can use it as needed.
// Check for attachments.
AttachmentType attachments =
// Enumerate through the attachments.
for (int attachmentCount = 0;
attachmentCount < attachments.Length;
// Form the GetAttachment request.
GetAttachmentType getAttachmentRequest =
// Add the attachment to retrieve.
// Make the GetAttachment request.
// We then parse through the
// GetAttachment response to get the
//attachment object itself
// We only passed in one AttachmentId.
// Therefore, we can assume that we
// got at most one attachment back.
if (attachmentResponseMessage != null)
== ResponseClassType.Success &&
AttachmentType attachment =
Now you have the message body and the attachments and you can do whatever you want to with them!
And that is that! Maybe not as easy as you may have hoped, but not too bad, either!
© 2007 Microsoft Corporation. All rights reserved.