Issue with getAttachmentContentAsync in Outlook Event-based Add-in

Diluka Hewage 31 Reputation points
2025-01-08T04:52:41.3766667+00:00

I have developed an event-based Outlook add-in to handle the OnMessageSend event. My goal is to access the attachment content when the event is triggered.

The getAttachmentsAsync method works fine; I can retrieve details like attachment name, ID, and size. However, when I call getAttachmentContentAsync, it does not return any content or error.

Here is the relevant code snippet from my add-in:

function onMessageSendHandler(event) {
    try {
       const item = Office.context.mailbox.item;
       const options = { asyncContext: { currentItem: item } };
     
       // Get attachments asynchronously
       item.getAttachmentsAsync(options, (result) => {
           if (result.status === Office.AsyncResultStatus.Failed) {
               console.log("Error getting attachments:", result.error.message);
               // Complete the event with success to allow sending
               event.completed({ allowEvent: true });
               return;
           }
     
           if (result.value.length <= 0) {
               console.log("Mail item has no attachments.");
               // Complete the event with success to allow sending
               event.completed({
                 allowEvent: false,
                 errorMessage: "Please add attachment(s)"
               });
               return;
           }
     
           const currentItem = result.asyncContext.currentItem;
         
           // Process each attachment
           for (let i = 0; i < result.value.length; i++) {    
               currentItem.getAttachmentContentAsync(result.value[i].id, (asyncResult) => {
                   console.log(asyncResult.status);
                   if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                       console.error("Error getting attachment content:", asyncResult.error.message);
                       return;
                   }    
                   console.log("Attachment content:", asyncResult.value.content);
               });
           }
     
           // Complete the event with success to allow sending
           event.completed({ allowEvent: true });
       });
    } catch (error) {
        console.log("Error Catched", error);
    }
}

// Associate the event handler
Office.actions.associate("onMessageSendHandler", onMessageSendHandler);

In this code:

  1. getAttachmentsAsync successfully retrieves attachment details.
  2. getAttachmentContentAsync is called with the attachment ID, but it does not return any content or error message.

I am unsure what is causing this behavior. Is there a limitation or a known issue with getAttachmentContentAsync in the OnMessageSend event?

Any insights or suggestions would be greatly appreciated!

Outlook
Outlook
A family of Microsoft email and calendar products.
4,295 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
1,019 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,141 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.