Berend Engelbrecht's avatar
Berend Engelbrecht
Brass Contributor
Apr 09, 2025
Status:
New

Implement Office.context.mailbox.item.getAsFileAsync for draft messages

We have a requirement in an Office.js add-in to do the following:

  • Automatically send a message
  • Save the sent message

The functionality was previously implemented using EWS and Office.context.mailbox.getCallbackTokenAsync. However, both are being phased out. Office.auth.getAccessToken is supposed to replace Office.context.mailbox.getCallbackTokenAsync, but we did not manage to get it to work in my add-in. To send the mail, Office.context.mailbox.item.sendAsync is now available, but it currently does not allow to execute code after sending. So we want to collect the draft before sending and process it in the back-end of our web app.

We are hindered here by the fact that Office.context.mailbox.item.getAsFileAsync cannot be used on a draft. The function is currently not available for a draft message, we get the error message `Office.context.mailbox.item.getAsFileAsync is not a function` when trying to use it. The same works fine for messages in other Outlook folders like Inbox or Sent Items.

To assemble eml-like text for the draft message so that we can save it in the back end of our add-in requires quite a lot of code. Also, img nodes reference external content, e.g., they have a src like `src="https://attachments.office.net/owa/.../service.svc/s/GetAttachmentThumbnail?id=` with id being a base64 string, where an eml should have `src="cid:` with cid referencing to a short id of a mime part in the same eml. There is no straightforward way to resolve all images when building an eml client-side.

4 Comments

  • bondo's avatar
    bondo
    Copper Contributor

     

    Same need from an add-in that archives mail to a document/project system.

     

    We file a user's replies in the project their thread belongs to. Today that only works after the user opens the sent copy in Sent Items, because getAsFileAsync is read mode only and there is no event after send. With getAsFileAsync in compose (for example inside OnMessageSend, or right before sendAsync) the add-in could file the exact MIME of the reply at send time, inline images as cid: parts, without Graph or EWS.

     

    One detail that matters for this: please return the internetMessageId that the message will carry when it is sent, or document whether it can change at send. Several issues report a change between draft and sent; two (#2659, #1244) carry a product-bug label, but Microsoft has not confirmed the cause or fixed it (in #1244 it could not reproduce it). Is that expected?

    - office-js #6066 (classic Outlook on Windows): https://github.com/OfficeDev/office-js/issues/6066

    - office-js #2659 (classic Outlook on Windows): https://github.com/OfficeDev/office-js/issues/2659

    - office-js #1244 (Outlook on Mac): https://github.com/OfficeDev/office-js/issues/1244

     

    The event after send that this pairs with is requested in the idea "Outlook add-ins: event after a message is sent, with the sent item's internetMessageId and itemId":

    - https://techcommunity.microsoft.com/idea/microsoft365developerplatform/outlook-add-ins-event-after-a-message-is-sent-with-the-sent-items-internetmessag/4577403

     

    Related: the request to get the Message-ID of a sent message, the idea "Retrieve RFC message ID after sending the email" and office-js #5528:

    - https://techcommunity.microsoft.com/idea/microsoft365developerplatform/retrieve-rfc-message-id-after-sending-the-email/4410862

    - https://github.com/OfficeDev/office-js/issues/5528

     

    (Drafted with the help of an AI assistant.)

  • I feel myself that API design could be more careful. It is peculiar and unexpected to a developer that function X operating on an email message can be used in the MessageReadCommandSurface but not in the MessageComposeCommandSurface. In Outlook you can save both a draft and a sent or received message in MIME format. I tried classic desktop and outlook.office.com web version.

  • ashishtank's avatar
    ashishtank
    Copper Contributor

    Indeed, this would be really helpful for lot many scenarios.