Search code examples
javascriptoutlookexchangewebservicesoutlook-addinoffice-addins

How to get the email content as EML or MSG from the exchange server using JavaScript?


I want to get the content of an email in the .msg format in my Node app. Currently I send the following SOAP request to get an html version of the email:

const getEmailContentSOAP = `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
    <soap:Header>
        <RequestServerVersion Version="Exchange2013"
          xmlns="http://schemas.microsoft.com/exchange/services/2006/types"
          soap:mustUnderstand="0" />
      </soap:Header>
  <soap:Body>
    <GetItem
      xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
      xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
      <ItemShape>
        <t:BaseShape>Default</t:BaseShape>
        <t:IncludeMimeContent>true</t:IncludeMimeContent>
      </ItemShape>
      <ItemIds>
        <t:ItemId Id="${emailID}" />
      </ItemIds>
    </GetItem>
  </soap:Body>
</soap:Envelope>`;

Is there a way to directly get the .msg version or convert this to .msg?


Solution

  • No the MSG format is an Office File format (Compound file format https://en.wikipedia.org/wiki/Compound_File_Binary_Format which is not trivial to generate) so generally using Outlook or Redemption is the only practical way of doing this along with the only real reason people usually try to use the MSG format is to maintain fidelity of MAPI properties and Attachment types which you will need MAPI for.

    For what you doing with EWS the content you get back with the IncludeMimeContent is the MIMEConent of the Message which can just be saved as an EML file and thus opened in any email client that support EML including Outlook which is generally sufficient for most thing (excluding Migration).