Click here to Skip to main content
15,897,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)
C#
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>


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?
Posted

1 solution

A succesful ExchangeService message has the below format:

XML
<?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>



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.
            }
          }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900