使用Exchange Web服务下载附件 [英] Downloading Attachments Using Exchange Web Service
本文介绍了使用Exchange Web服务下载附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从特定邮箱下载/访问附件.我正在尝试使用以下代码,但是没有任何附件. (可能是因为AttachmentID不正确)
I want to download/ access attachment from particular mailbox. I am trying to do using following code however I am not getting any attachment. (may be because AttachmentID''s are not proper)
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.RequestServerVersionValue = new RequestServerVersion();
esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;
esb.Credentials = new NetworkCredential("<user_id>", "<password>", "<domain>");
esb.Url = @"https://<server_fqdn>/ews/Exchange.asmx";
GetAttachmentType request = new GetAttachmentType();
// Create the response shape.
AttachmentResponseShapeType responseShape = new AttachmentResponseShapeType();
responseShape.BodyType = BodyTypeResponseType.Best;
responseShape.BodyTypeSpecified = true;
// Add the response shape to the request.
request.AttachmentShape = responseShape;
// Identify the attachment IDs to get.
RequestAttachmentIdType[] ids = new RequestAttachmentIdType[2];
ids[0] = new RequestAttachmentIdType();
ids[1] = new RequestAttachmentIdType();
ids[0].Id = "AAAlAE1BQG1";
ids[1].Id = "AAAlAE1Bas";
//// Add the attachment IDs to the request.
request.AttachmentIds = ids;
GetAttachmentResponseType response = esb.GetAttachment(request);
ResponseMessageType[] rmta = response.ResponseMessages.Items;</server_fqdn></domain></password></user_id>
我不知道附件ID到底是什么,我需要处理所有附件,所以我怎么知道要指定哪个ID?
I don''t know what exactly Attachment ID is and I need to process all attachment , So how do I know which ID to specify?
推荐答案
成功的ExchangeService消息具有以下格式:
A succesful ExchangeService message has the below format:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<t:ServerVersionInfo MajorVersion="8" MinorVersion="0" MajorBuildNumber="595" MinorBuildNumber="0"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" />
</soap:Header>
<soap:Body>
<FindItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
<m:ResponseMessages>
<m:FindItemResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:RootFolder TotalItemsInView="10" IncludesLastItemInRange="true">
<t:Items>
<t:Message>
<t:ItemId Id="AS4AUn=" ChangeKey="fsVU4==" />
</t:Message>
<t:Message>
<t:ItemId Id="AS4AUM=" ChangeKey="fsVUA==" />
</t:Message>
</t:Items>
</m:RootFolder>
</m:FindItemResponseMessage>
</m:ResponseMessages>
</FindItemResponse>
</soap:Body>
</soap:Envelope>
要提取附件,ews.dll中的FindItemResponseMessageType类起主要作用.
To extract the attachement, FindItemResponseMessageType class from ews.dll plays the major role.
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.RequestServerVersionValue = new RequestServerVersion();
esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;
esb.Credentials = new NetworkCredential("<user_id>", "<password>", "<domain>");
esb.Url = @"https://<server_fqdn>/ews/Exchange.asmx";
..........
FindItemResponseType findItemResponse = esb.FindItem(findItemRequest);
foreach(ResponseMessageType responseMessage in findItemResponse.ResponseMessages)
{
if (responseMessage is FindItemResponseMessageType)
{
FindItemResponseMessageType firmt = (responseMessage as FindItemResponseMessageType);
FindItemParentType fipt = firmt.RootFolder;
object obj = fipt.Item;
// Determine whether the FindItem response contains grouped items.
if (obj is ArrayOfGroupedItemsType)
{
ArrayOfGroupedItemsType groupedItems = (obj as ArrayOfGroupedItemsType);
//TODO: Write code to handle grouped items.
}
// FindItem contains an array of items.
else if (obj is ArrayOfRealItemsType)
{
ArrayOfRealItemsType items = (obj as ArrayOfRealItemsType);
//TODO: Write code to handle items.
}
}
这篇关于使用Exchange Web服务下载附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文