-->

How can I retrieve the full MIME message from an M

2020-07-31 19:07发布

问题:

I am developing an Outlook Web Access Add-In in which I want to access all the data of an email including sender address, recipient address, subject and sent date etc. I also want to download the entire message file. I have found one Outlook Web REST API but don't know how to use it.

Can anyone explain how to use this API to retrieve the entire MIME message and provide an example?

回答1:

1. To get the message details you can use the javascript APIs available in office.js. Refer the link for details on individual APIs that are available on an item:

https://dev.office.com/reference/add-ins/outlook/1.5/Office.context.mailbox.item?product=outlook&version=v1.5

2. To get the entire message file. GetMessage API does not provide you with a .msg file but it will fetch you all the properties of the message, you can use this to get extra details about the message which are not provided by the javascript APIs directly.

3. There is a way to get a .eml file using the following two steps:

Step1: Get a EWS callback token using the JS API getCallbackTokenAsync([options], callback). Refer link:

https://dev.office.com/reference/add-ins/outlook/1.5/Office.context.mailbox?product=outlook&version=v1.5

Step2: With this token from your server make an EWS request to get the item with IncludeMimeContent set to true and save the response as a .eml file. Refer link:

https://msdn.microsoft.com/en-us/library/office/aa566013(v=exchg.150).aspx



回答2:

I guess you want to get the current email in MIME format (aka *.eml file type). If so, see the answer.

1. Exchange Web Services (EWS). Prior 2019 year

You have to use Exchange Web Services (EWS) and there are two ways:

  1. Do it in JavaScript via makeEwsRequestAsync() method by setting IncludeMimeContent property in the request and process content of the MimeContent tag of the response (it's base64, so you may need to decode).

    But it doesn't work on iOS/Android (link) and the email size limit for JavaScript APIs is 1 MB (see all limitations of the JavaScript API), so the implementation of fetching and decoding the email must run on the back-end (which may be an unnecessary load for the server). Hence you may forget option #1 and start reading the next option.

  2. Send EWS request and process the response on the back-end. But for that you need to get a callback token (for authentication from your back-end), item ID of the email and the EWS url (see this post on how to get them in the add-in).

Having said that, there are said news. Since July 2018 Microsoft issues only security updates for EWS (see the official statement). They phase it out in favour of Microsoft Graph, the new gateway to Office data, which so far does NOT support export of email in the EML format. The feature is promised in beta by the end of 2018.

2. Microsoft Graph. Starting from 2019 year

EWS is dead and Microsoft Graph is the way to go. The Microsoft folks promised to expose the entire email MIME stream in Outlook Mail REST API (should be in beta by now). So keep an eye on Outlook mail REST API.

Note: MS Graph works only with Exchange Online (Office 365 in the cloud) or on Exchange on-premises in a hybrid deployment (requires at least Exchange 2016 Cumulative Update 3 (CU3) on-premises server integrated with Office 365).