次の方法で共有


How to get the body of the email message using Exchange 2007 Web services

Futher to SGriffin's Weblog as below:
https://blogs.msdn.com/stephen_griffin/archive/2007/02/09/exchange-web-services-and-internet-message-headers.aspx

in order to access the body of an email message, the MAPI property PR_BODY_HTML (0x1013) could be used.
However, we need to remember the property is binary in nature and base64 encoded.
Here comes Convert.FromBase64String, System.Text.ASCIIEncoding to our rescue using which the ASCII of the body could be got, as below:

byte [] bAscii = Convert.FromBase64String(it.ExtendedProperty[0].Item.ToString());
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string str = enc.GetString(bAscii);

/* Source Code */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Serialization;
using WebServices1.localhost;

namespace WebServices1
{
class Program
{
static void Main(string[] args)
{
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Credentials = new NetworkCredential("username", "password", "domain");
esb.Url = @https://localhost/EWS/Exchange.asmx;
DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
folderIDArray[0] = new DistinguishedFolderIdType();
folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
//set the props that we want to retrieve
FolderResponseShapeType frst = new FolderResponseShapeType();
frst.BaseShape = DefaultShapeNamesType.AllProperties;
GetFolderType myfoldertype = new GetFolderType();
myfoldertype.FolderIds = folderIDArray;
myfoldertype.FolderShape = frst;
Console.WriteLine("Getting inbox");
GetFolderResponseType myFolder = esb.GetFolder(myfoldertype);
FolderInfoResponseMessageType firmtInbox = (FolderInfoResponseMessageType)myFolder.ResponseMessages.Items[0];
Console.WriteLine("got folder: {0}", firmtInbox.Folders[0].DisplayName);
PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType();
ptuftSubject.FieldURI = UnindexedFieldURIType.itemSubject;
PathToExtendedFieldType pteftFlagStatus = new PathToExtendedFieldType();
pteftFlagStatus.PropertyTag = "0x1013"; // PR_BODY_HTML
pteftFlagStatus.PropertyType = MapiPropertyTypeType.Binary;
FindItemType findItemRequest = new FindItemType();
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
findItemRequest.ItemShape = new ItemResponseShapeType();
findItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
findItemRequest.ItemShape.AdditionalProperties = new BasePathToElementType[2];
findItemRequest.ItemShape.AdditionalProperties[0] = ptuftSubject;
findItemRequest.ItemShape.AdditionalProperties[1] = pteftFlagStatus;
findItemRequest.ParentFolderIds = new FolderIdType[] { firmtInbox.Folders[0].FolderId };
FindItemResponseType firt = esb.FindItem(findItemRequest);
foreach (FindItemResponseMessageType firmtMessage in firt.ResponseMessages.Items)
{
if (null != firmtMessage.RootFolder && firmtMessage.RootFolder.TotalItemsInView > 0)
{
foreach (ItemType it in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items)
{
Console.WriteLine("got item subject: {0}", it.Subject);
if (null != it.ExtendedProperty)
{
Console.WriteLine("Prop PR_BODY_HTML(Binary): {0}", it.ExtendedProperty[0].Item.ToString());
byte [] bAscii = Convert.FromBase64String(it.ExtendedProperty[0].Item.ToString());
Console.WriteLine();
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string str = enc.GetString(bAscii);
Console.WriteLine("Prop PR_BODY_HTML(ASCII): {0}", str);
}
else
{
Console.WriteLine("Prop PR_BODY_HTML: not found");
}
}
}
}
Console.WriteLine("\nHit any key to continue");
Console.ReadKey(true);
}
}
}