Allow .msg file export through JS add-in API in Outlook-Desktop

Allow .msg file export through JS add-in API in Outlook-Desktop
13

Upvotes

Upvote

 Jun 29 2022
17 Comments (17 New)
New

I'd like to create an add-in which create a .msg file of an email and sends it to a remote server for archival purposes.
Outlook already can create .msg files through the "Save as..." dialog but this functionality is not exposed to the Office API.

I could access all relevant information through the API and create an .eml file for example (or use EWS/graph-API for that), but I'd prefer to leverage the existing outlook functionality.

 

I suggest a method "getMsgFileAsync( options: Office.AsyncContextOptions, callback?: (asyncResult: Office.AsyncResult<ArrayBuffer>) => void): void" on the "Office.context.mailbox.item" object.

As far as permissions go, "ItemRead" should suffice.

Comments
Copper Contributor

Any chance on a comment on this one?

Is this possible or is it something that the Office API will never look to support?

Thanks!

Copper Contributor

I am also interested in this topic.

It would be a great feature to have because it would make it so much easier to create an EML and send to a 3rd party server. We always struggle implementing this, because we must support this on every platform and there are lot of code challanges on different platforms.

Also it would be great to have this possibility on compose sidebar as well while composing an email.

Thanks!

Copper Contributor

This ability is critical to our workflow and with the new version of Outlook killing off VSTO and VBA add-in options there is no other way that I've found to accomplish what we've been doing for decades. Please consider adding this capability prior to release of the "New Outlook". 

Copper Contributor

I need this feature as soon as possible !

Copper Contributor

Me to !

Copper Contributor

A native API would be fantastic! If you can't wait, as mentioned by OP, a workaround is to implement the EML format yourself (which is really just an IMF text file). It was a bit annoying, but doable (and totally worth it!)...

 

  1. item.getAllInternetHeadersAsync()
  2. extract MIME boundary from headers to separate messages below
  3. item.body.getAsync(Office.CoercionType.Html) & base64 encode
  4. item.body.getAsync(Office.CoercionType.Text) & base64 encode
  5. iterate through item.attachments & item.getAttachmentContentAsync()
  6. upload file
Copper Contributor

@Rich-E do you have a working example of this somewhere for reference? 

Copper Contributor

@Rich-E, save us!  :smile:

Copper Contributor

just took a look at the code--it's not the prettiest thing right now :xd:


give me some time to clean it up before i shove it somewhere!

Copper Contributor

Alrighty @Jim_Neill @Philbewan here you go:
https://gist.github.com/DevDuck/db4e7e5903d5d3d2d49a238871da4311

 

Call it this way:

try {
  await getEmailContentAsync({}, async (asyncResult: Office.AsyncResult<string>) => {
    if (asyncResult.status != Office.AsyncResultStatus.Succeeded) {
      // handle error
      return;
    }

    let content = asyncResult.value;

    // upload file
    // Buffer.from(content, "utf8");
  };
}
catch (err) {
  // handle exception
}
      

 

It's a bit messy but should work. Please let me know if you have any issues with it. I know of one sporadic issue with the Mail app opening the files but Outlook seems to open them fine.

Copper Contributor

Huge help, and very kind of you to share. Will give this some testing and hopefully will fit my needs for replacing an old VBA solution. 

Copper Contributor

Thank you, @Rich-E - you have been a great help!

Copper Contributor

It looks like Microsoft has added an Outlook Javascript API preview method for generating a .eml file here:
office.messageread

Copper Contributor

@Philbewan Fantastic! Looks like it's only available on Windows at the moment. Wonder how long it will take to exit Preview...

Copper Contributor

From an Enterprise Archive's perspective it would be fantastic to be able to retrieve the original MSG as it is instead of creating a new EML based on data from the API. 

Copper Contributor

I completely agree. We had that ability in old vba, you would think they wouldn't jsut completely kill functionality.

Copper Contributor

Hi Rich-E

As you have mentioned i have used the same code that is working fine in Read Mode, but my requirement is to save the mail in compose mode also, i am new to Outlook Add in any help will be great for me