简体   繁体   中英

How can I get attachment content from an Outlook mail in a Node.js Outlook add-in?

First of all, being a student, I lack some experience to understand everything about what I tried and what I try. But still, I feel like I've tried pretty much every solution available on the net.

I'm developing a Yeoman based Outlook add-in (full javascript with Node.js) for my company, and I came to the point where I need to get the attachments from a mail, not just the informations like size, file type or name of the attachments, I need the full content, in base64 if possible (in order to produce a .eml file containing the mail body and attached files).

I've found multiple ways to do so, problem is none of them work :

  • first I tried with the messy microsoft documentation, and the first solution that I found is this one : https://docs.microsoft.com/en-us/office/dev/add-ins/outlook/get-attachments-of-an-outlook-item , problem is that with this solution I need to make use of c#, but my add-in is full js, and I need to find a solution that works with JS and Node.js
  • then I looked for a solution that allows me to communicate a way or another with the Exchange Server to get the attachments content, and I found this : https://github.com/gautamsi/ews-javascript-api , but this was not working with browser oriented add-in (and I need my add-in to work on both client and browsers), so I made use of this : https://github.com/gautamsi/ews-js-api-browser . I found some code samples in the first link and here : Save outlook Email attachment using ews-javascript-api npm in node JS that helped me build something (using the second link's example's syntax on the third link's sample code) that was supposed to work, but coming to the point where I'm trying to get the attachments from the mail, I get a lot of informations from the attachment, but anything related to it's content is null, and the next step where I "Load" the file returns an error 500 from the "service" with "the document is empty", and I still don't know why yet.
  • finally, I tried to take a step back, looking on google for office api and how to get attachment content, and I've found this link : https://blog.mastykarz.nl/office-365-unified-api-mail/ that almost looked to good to be true, and I was right : according to microsoft doc, I need a token in order to make work the office 365 api url, token given by Azure AD when an administrator authorizes you to obtain it (or so do I think, this is the part where the microsoft doc is the messier). Problem is, while connecting on Azure AD, I get an error displaying on the screen from the website (absolutely no details about the error), and some places that I need to access in order to get my token just wont work, displaying another error message without any details or just never loading.

I've reduced as much as possible the size of my question, not wanting to drown you under informations, sample codes, links and pics so, sorry if it may look both too long and too poor in details.

Thank you for your time, feel free to give any solution, suggestion or advice that come to your mind !

doing the same thing, solution I found:

  • Get exchangeToken in client ( Office.context.mailbox.getCallbackTokenAsync() )
  • Send token, attachmentsIds, emailID to server (C# in my case, nodejs in your case)
  • Use EWS Managed API 2.0 or in your case ews-javascript-api to handle loading email mimecontent (contains email + attachments) or attachment mimecontent.
  • Upload file to another service or do whatever you want with it.

Before I switched to C# for serverside I was using a nodejs one for testing, so if you have questions feel free to ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM