You can get the attachments of an Outlook item in a couple of ways but which option you use depends on your scenario.
Send the attachment information to your remote service.
Your add-in 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.
Use the getAttachmentContentAsync API, available from requirement set 1.8. Supported formats: AttachmentContentFormat.
This API may be handy if EWS/REST is unavailable (for example, due to the admin configuration of your Exchange server), or your add-in wants to use the base64 content directly in HTML or JavaScript. Also, the
getAttachmentContentAsync
API is available in compose scenarios where the attachment may not have synced to Exchange yet; see Manage an item's attachments in a compose form in Outlook to learn more.
This article elaborates on the first option. To send attachment information to the remote service, use the following properties and function.
Get help for Microsoft To Do. If you have a question or need assistance, the best resource is our in-app support center. You'll be able to search our FAQ, contact support, or suggest a feature directly within the app. Want to buy Microsoft Office or download Office 365? Office apps like Word, Excel & PowerPoint are now part of Microsoft 365 at no extra cost.
Office.context.mailbox.ewsUrl property – Provides the URL of Exchange Web Services (EWS) on the Exchange server that hosts the mailbox. Your service uses this URL to call the ExchangeService.GetAttachments method, or the GetAttachment EWS operation.
Office.context.mailbox.item.attachments property – Gets an array of AttachmentDetails objects, one for each attachment to the item.
Office.context.mailbox.getCallbackTokenAsync function – Makes an asynchronous call to the Exchange server that hosts the mailbox to get a callback token that the server sends back to the Exchange server to authenticate a request for an attachment.
Using the attachments API
To use the attachments API to get attachments from an Exchange mailbox, perform the following steps.
Show the add-in when the user is viewing a message or appointment that contains an attachment.
Get the callback token from the Exchange server.
Send the callback token and attachment information to the remote service.
Get the attachments from the Exchange server by using the
ExchangeService.GetAttachments
method or theGetAttachment
operation.
Each of these steps is covered in detail in the following sections using code from the Outlook-Add-in-JavaScript-GetAttachments sample.
Note
The code in these examples has been shortened to emphasize the attachment information. The sample contains additional code for authenticating the add-in with the remote server and managing the state of the request.
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 an add-in 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.
Send attachment information to the remote service
The remote service that your add-in calls defines the specifics of how you should send the attachment information to the service. In this example, the remote service is a 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.
The Office.context.mailbox.item.attachments
property contains a collection of AttachmentDetails
objects, one for each attachment to the item. In most cases, the add-in 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.
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.
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.
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.
Microsoft Office Install
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.
Get Microsoft Office Online
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.