Export (0) Print
Expand All

How to: Get attachments of an Outlook item from an Exchange server

apps for Office

Use a mail app to enable a service to get attachments from an Exchange server.

Last modified: February 27, 2015

Applies to: Access app for SharePoint | apps for Office | Excel | Outlook | PowerPoint | Project | Word

Learn more about supported hosts and other requirements.

A mail app cannot pass the attachments of a selected item directly to the remote service that runs on your server. Instead, the mail app can use the attachments API to send information about the attachments to the remote service. The service can then contact the Exchange server directly to retrieve the attachments.

To send attachment information to the remote service, you use the following properties and function:

To use the attachments API to get attachments from an Exchange mailbox, perform the following steps:

  1. Show the mail app when the user is viewing a message or appointment that contains an attachment.

  2. Get the callback token from the Exchange server.

  3. Send the callback token and attachment information to the remote service.

  4. Get the attachments from the Exchange server by using the ExchangeService.GetAttachments method or the GetAttachment operation.

Each of these steps is covered in detail in the following sections using code from the Mail apps for Office: Get attachments from an Exchange server sample.

Note Note

The code in these examples has been shortened to emphasize the attachment information. The sample contains additional code for authenticating the mail app with the remote server and managing the state of the request.

Activate the mail app

You can use an ItemHasAttachment rule in the mail app manifest file to show your mail app when the selected item has attachments, as shown in the following example.

<Rule xsi:type="ItemHasAttachment" />

Get a callback token

The Office.context.mailbox object provides the getCallbackTokenAsync function to get a token that the remote server can use to authenticate with the Exchange server. The following code shows a function in a mail app that starts the asynchronous request to get the callback token, and the callback function that gets the response. The callback token is stored in the service request object that is defined in the next section.

function getAttachmentToken() {
    if (serviceRequest.attachmentToken == "" {
function attachmentTokenCallback(asyncResult, userContext) {
    if (asyncResult.status === "success") {
        // Cache the result from the server.
        serviceRequest.attachmentToken = asyncResult.value;
        serviceRequest.state = 3;
    } else {
        showToast("Error", "Could not get callback token: " + asyncResult.error.message);

Send attachment information to the remote service

The remote service that your mail app calls defines the specifics of how you should send the attachment information to the service. In this example, the remote service is Web API application created by using Visual Studio 2013. The remote service expects the attachment information in a JSON object. The following code initializes an object that contains the attachment information.

// Initialize a context object for the app.
//   Set the fields that are used on the request
//   object to default values.
serviceRequest = new Object();
serviceRequest.attachmentToken = "";
serviceRequest.ewsUrl = Office.context.mailbox.ewsUrl;
serviceRequest.attachments = new Array();

The Office.context.mailbox.item.attachments property contains a collection of AttachmentDetails objects, one for each attachment to the item. In most cases, the mail app can pass just the attachment ID property of an AttachmentDetails object to the remote service. If the remote service needs more details about the attachment, you can pass all or part of the AttachmentDetails object. The following code defines a method that puts the entire AttachmentDetails array in the serviceRequest object and sends a request to the remote service.

    function makeServiceRequest() {
      // Format the attachment details for sending.
      for (var i = 0; i < mailbox.item.attachments.length; i++) {
        serviceRequest.attachments[i] = JSON.parse(JSON.stringify(mailbox.item.attachments[i].$0_0));

        url: '../../api/Default',
        type: 'POST',
        data: JSON.stringify(serviceRequest),
        contentType: 'application/json;charset=utf-8'
      }).done(function (response) {
        if (!response.isError) {
          var names = "<h2>Attachments processed using " +
                        serviceRequest.service +
                        ": " +
                        response.attachmentsProcessed +
          for (i = 0; i < response.attachmentNames.length; i++) {
            names += response.attachmentNames[i] + "<br />";
          document.getElementById("names").innerHTML = names;
        } else {
          app.showNotification("Runtime error", response.message);
      }).fail(function (status) {

      }).always(function () {
        $('.disable-while-sending').prop('disabled', false);

Get the attachments from the Exchange server

Your remote service can use either the GetAttachments EWS Managed API method or the GetAttachment EWS operation to retrieve attachments from the server. The service application needs two objects to deserialize the JSON string into .NET Framework objects that can be used on the server. The following code shows the definitions of the deserialization objects.

namespace AttachmentsSample
  public class AttachmentSampleServiceRequest
    public string attachmentToken { get; set; }
    public string ewsUrl { get; set; }
    public string service { get; set; }
    public AttachmentDetails[] attachments { get; set; }

  public class AttachmentDetails
    public string attachmentType { get; set; }
    public string contentType { get; set; }
    public string id { get; set; }
    public bool isInline { get; set; }
    public string name { get; set; }
    public int size { get; set; }

Use the EWS Managed API to get the attachments

If you use the EWS Managed API in your remote service, you can use the GetAttachments method, which will construct, send, and receive an EWS SOAP request to get the attachments. We recommend that you use the EWS Managed API because it requires fewer lines of code and provides a more intuitive interface for making calls to EWS. The following code makes one request to retrieve all the attachments, and returns the count and names of the attachments processed.

    private AttachmentSampleServiceResponse GetAtttachmentsFromExchangeServerUsingEWSManagedApi(AttachmentSampleServiceRequest request)
      var attachmentsProcessedCount = 0;
      var attachmentNames = new List<string>();

      // Create an ExchangeService object, set the credentials and the EWS URL.
      ExchangeService service = new ExchangeService();
      service.Credentials = new OAuthCredentials(request.attachmentToken);
      service.Url = new Uri(request.ewsUrl);

      var attachmentIds = new List<string>();

      foreach (AttachmentDetails attachment in request.attachments)

      // Call the GetAttachments method to retrieve the attachments on the message.
      // This method results in a GetAttachments EWS SOAP request and response
      // from the Exchange server.
      var getAttachmentsResponse =
                               new PropertySet(BasePropertySet.FirstClassProperties,

      if (getAttachmentsResponse.OverallResult == ServiceResult.Success)
        foreach (var attachmentResponse in getAttachmentsResponse)

          // Write the content of each attachment to a stream.
          if (attachmentResponse.Attachment is FileAttachment)
            FileAttachment fileAttachment = attachmentResponse.Attachment as FileAttachment;
            Stream s = new MemoryStream(fileAttachment.Content);
            // Process the contents of the attachment here.

          if (attachmentResponse.Attachment is ItemAttachment)
            ItemAttachment itemAttachment = attachmentResponse.Attachment as ItemAttachment;
            Stream s = new MemoryStream(itemAttachment.Item.MimeContent.Content);
            // Process the contents of the attachment here.


      // Return the names and number of attachments processed for display
      // in the mail app UI.
      var response = new AttachmentSampleServiceResponse();
      response.attachmentNames = attachmentNames.ToArray();
      response.attachmentsProcessed = attachmentsProcessedCount;

      return response;

Use EWS to get the attachments

If you use EWS in your remote service, you need to construct a GetAttachment SOAP request to get the attachments from the Exchange server. The following code returns a string that provides the SOAP request. The remote service uses the String.Format method to insert the attachment ID for an attachment into the string.

    private const string GetAttachmentSoapRequest =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
<t:RequestServerVersion Version=""Exchange2013"" />
    <GetAttachment xmlns=""http://schemas.microsoft.com/exchange/services/2006/messages""
        <t:AttachmentId Id=""{0}""/>

Finally, the following method does the work of using an EWS GetAttachment request to get the attachments from the Exchange server. This implementation makes an individual request for each attachment, and returns the count of attachments processed. Each response is processed in a separate ProcessXmlResponse method, defined next.

    private AttachmentSampleServiceResponse GetAttachmentsFromExchangeServerUsingEWS(AttachmentSampleServiceRequest request)
      var attachmentsProcessedCount = 0;
      var attachmentNames = new List<string>();

      foreach (var attachment in request.attachments)
        // Prepare a web request object.
        HttpWebRequest webRequest = WebRequest.CreateHttp(request.ewsUrl);
          string.Format("Bearer {0}", request.attachmentToken));
        webRequest.PreAuthenticate = true;
        webRequest.AllowAutoRedirect = false;
        webRequest.Method = "POST";
        webRequest.ContentType = "text/xml; charset=utf-8";

        // Construct the SOAP message for the GetAttachment operation.
        byte[] bodyBytes = Encoding.UTF8.GetBytes(
          string.Format(GetAttachmentSoapRequest, attachment.id));
        webRequest.ContentLength = bodyBytes.Length;

        Stream requestStream = webRequest.GetRequestStream();
        requestStream.Write(bodyBytes, 0, bodyBytes.Length);

        // Make the request to the Exchange server and get the response.
        HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

        // If the response is okay, create an XML document from the reponse
        // and process the request.
        if (webResponse.StatusCode == HttpStatusCode.OK)
          var responseStream = webResponse.GetResponseStream();

          var responseEnvelope = XElement.Load(responseStream);

          // After creating a memory stream containing the contents of the 
          // attachment, this method writes the XML document to the trace output.
          // Your service would perform it's processing here.
          if (responseEnvelope != null)
            var processResult = ProcessXmlResponse(responseEnvelope);
            attachmentNames.Add(string.Format("{0} {1}", attachment.name, processResult));


          // Close the response stream.

        // If the response is not OK, return an error message for the 
        // attachment.
          var errorString = string.Format("Attachment \"{0}\" could not be processed. " +
            "Error message: {1}.", attachment.name, webResponse.StatusDescription);

      // Return the names and number of attachments processed for display
      // in the mail app UI.
      var response = new AttachmentSampleServiceResponse();
      response.attachmentNames = attachmentNames.ToArray();
      response.attachmentsProcessed = attachmentsProcessedCount;

      return response;

Each response from the GetAttachment operation is sent to the ProcessXmlResponse method. This method checks the response for errors. If it doesn't find any errors, it processes file attachments and item attachments. The ProcessXmlResponse method performs the bulk of the work to process the attachment.

    // This method processes the response from the Exchange server.
    // In your application the bulk of the processing occurs here.
    private string ProcessXmlResponse(XElement responseEnvelope)
      // First, check the response for web service errors.
      var errorCodes = from errorCode in responseEnvelope.Descendants
                       select errorCode;
      // Return the first error code found.
      foreach (var errorCode in errorCodes)
        if (errorCode.Value != "NoError")
          return string.Format("Could not process result. Error: {0}", errorCode.Value);

      // No errors found, proceed with processing the content.
      // First, get and process file attachments.
      var fileAttachments = from fileAttachment in responseEnvelope.Descendants
                            select fileAttachment;
      foreach(var fileAttachment in fileAttachments)
        var fileContent = fileAttachment.Element("{http://schemas.microsoft.com/exchange/services/2006/types}Content");
        var fileData = System.Convert.FromBase64String(fileContent.Value);
        var s = new MemoryStream(fileData);
        // Process the file attachment here. 

      // Second, get and process item attachments.
      var itemAttachments = from itemAttachment in responseEnvelope.Descendants
                            select itemAttachment;
      foreach(var itemAttachment in itemAttachments)
        var message = itemAttachment.Element("{http://schemas.microsoft.com/exchange/services/2006/types}Message");
        if (message != null)
         // Process a message here.
        var calendarItem = itemAttachment.Element("{http://schemas.microsoft.com/exchange/services/2006/types}CalendarItem");
        if (calendarItem != null)
          // Process calendar item here.
        var contact = itemAttachment.Element("{http://schemas.microsoft.com/exchange/services/2006/types}Contact");
        if (contact != null)
          // Process contact here.
        var task = itemAttachment.Element("{http://schemas.microsoft.com/exchange/services/2006/types}Tontact");
        if (task != null)
          // Process task here.
        var meetingMessage = itemAttachment.Element("{http://schemas.microsoft.com/exchange/services/2006/types}MeetingMessage");
        if (meetingMessage != null)
          // Process meeting message here.
        var meetingRequest = itemAttachment.Element("{http://schemas.microsoft.com/exchange/services/2006/types}MeetingRequest");
        if (meetingRequest != null)
          // Process meeting request here.
        var meetingResponse = itemAttachment.Element("{http://schemas.microsoft.com/exchange/services/2006/types}MeetingResponse");
        if (meetingResponse != null)
          // Process meeting response here.
        var meetingCancellation = itemAttachment.Element("{http://schemas.microsoft.com/exchange/services/2006/types}MeetingCancellation");
        if (meetingCancellation != null)
          // Process meeting cancellation here.
      return string.Empty;

© 2015 Microsoft