Issue with getAttachmentContentAsync in Outlook Event-based Add-in

Diluka Hewage 56 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!

JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
1,029 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,173 questions
Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
5,420 questions
{count} votes

Accepted answer
  1. Joan Hua-MSFT 3,965 Reputation points Microsoft Vendor
    2025-01-17T05:11:08.17+00:00

    Hi @Diluka Hewage 

    Glad to hear this issue has been resolved and thanks for the sharing! 


    However, due to a recent update in forum policy, the question author now is not able to accept their own answers.  

    So, I have written a brief summary of the solution this issue. Please feel free to ACCEPT it as the Answer, which would benefit others who also have similar issues in the forum.  

    Issue: 

    When calling getAttachmentContentAsync, it does not return any content or error.

    Solution: 

    Instead of using getAttachmentContentAsync directly within the OnMessageSend event, using it inside the onMessageAttachmentsChanged event. When the onMessageAttachmentsChanged event is triggered, you can successfully access the attachment content using getAttachmentContentAsync.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Diluka Hewage 56 Reputation points
    2025-01-17T04:31:24.1366667+00:00

    I found a solution to this issue!

    Instead of using getAttachmentContentAsync directly within the OnMessageSend event, I used it inside the onMessageAttachmentsChanged event. When the onMessageAttachmentsChanged event is triggered, I can successfully access the attachment content using getAttachmentContentAsync.

    I’m not sure why getAttachmentContentAsync doesn’t work as expected in the OnMessageSend event, but using it in the onMessageAttachmentsChanged event resolved the problem for me.

    1 person found this answer helpful.

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.