Issue with getAttachmentContentAsync in Outlook Event-based Add-in
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:
-
getAttachmentsAsync
successfully retrieves attachment details. -
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!