Search code examples
graphoffice-jsoutlook-addinoffice-addinsoutlook-web-addins

Using Graph in Outlook addin to read email in MIME format


I am getting lost with Outlook addin development and really need some help. I have developed an addin that sends selected email to another server via REST API and it worked fine, but there was a limitation to 1MB so I tried to develop a solution that use ewsURL + SOAP but faced with CORS issues.

Now I got a suggestion to use GRAPH approach (fine with me) but I have no idea how that suppose to work using JavaScript.

Basically I need to get an email as MIME/EML format.

I was guided to check this article: https://learn.microsoft.com/en-us/graph/outlook-get-mime-message

There is endpoint that looks promissing:

https://graph.microsoft.com/v1.0/me/messages/4aade2547798441eab5188a7a2436bc1/$value

But I do not see explanation

  1. how to make authorization process?
  • I have tried to get token from getCallbackTokenAsync but that did not work
  • I have tried Office.context.auth.getAccessTokenAsync but getting an issue:

Error code: 13000 Error name: API Not Supported. Error message: The identity API is not supported for this add-in.

  1. how to get email id
  • I have tried to do Office.context.mailbox.item.itemId but it looks different compare to what I have seen in the examples (but hopefully that is not a problem)

Please help :-)


Solution

  • There are 2 solutions here. It is preferred longer term to use graph end point with https://learn.microsoft.com/en-us/office/dev/add-ins/develop/authorize-to-microsoft-graph and you can use https://graph.microsoft.com/v1.0/me/messages/4aade2547798441eab5188a7a2436bc1/$value. However this solution requires a backend / service . Transferring through backend is preferable for large content so the content can transfer directly from Exchange to the service.

    Alternatively, you can get token from getCallbackTokenAsync, from this doc: https://learn.microsoft.com/en-us/office/dev/add-ins/outlook/use-rest-api As you noted is that you will need to translate the ews id using convertToRestId. Putting together, your solution should look something like this:

    Office.context.mailbox.getCallbackTokenAsync({isRest: true}, function(result){
      if (result.status === "succeeded") {
        let token = result.value;
        var ewsItemId = Office.context.mailbox.item.itemId;
    
        const itemId = Office.context.mailbox.convertToRestId(
            ewsItemId,
            Office.MailboxEnums.RestVersion.v2_0);
    
        // Request the message's attachment info
        var getMessageUrl = Office.context.mailbox.restUrl +
            '/v2.0/me/messages/' + itemId + '/$value';
    
        var xhr = new XMLHttpRequest();
        xhr.open('GET', getMessageUrl);
        xhr.setRequestHeader("Authorization", "Bearer " + token);
        xhr.onload = function (e) {
           console.log(this.response);
        }
        xhr.onerror = function (e) {
           console.log("error occurred");
        }
        xhr.send();
      }
    

    });