I'm developing a web add-in for the new Outlook and need to send the currently selected email to a specific email address, including any attachments. I used the Yeoman Office generator to create the web add-in. To send the email, I used Office.context.mailbox.displayNewMessageForm. However, I'm having trouble with attachments. When I set the attachment type to 'file', it doesn't work. It works with type 'item', but the mail attachment is displayed as an item and opens like an Outlook item.
Here's my code:
const subject = 'Test Subject';
const item = Office.context.mailbox.item;
console.log(item);
const itemId = Office.context.mailbox.item.itemId;
// Separate inline and regular attachments
const inlineAttachments = attachments.filter(attachment => attachment.isInline);
const regularAttachments = attachments.filter(attachment => !attachment.isInline);
// Convert the attachment IDs to REST IDs
const inlineRestIds = inlineAttachments.map(attachment =>
Office.context.mailbox.convertToRestId(attachment.id, Office.MailboxEnums.RestVersion.v2_0)
);
const regularRestIds = regularAttachments.map(attachment =>
Office.context.mailbox.convertToRestId(attachment.id, Office.MailboxEnums.RestVersion.v2_0)
);
// Get the email subject and body
const originalSubject = item.subject;
item.body.getAsync("html", (bodyResult) => {
if (bodyResult.status === Office.AsyncResultStatus.Succeeded) {
const originalBody = bodyResult.value;
const emailHtmlBody = `
<p><strong>Original Subject:</strong> ${originalSubject}</p>
<p><strong>Original Body:</strong></p>
${originalBody}
`;
// Open a pre-filled compose window
Office.context.mailbox.displayNewMessageForm({
to: ["[email protected]"],
subject: subject,
htmlBody: emailHtmlBody,
attachments: [
...inlineRestIds.map((restId, index) => ({
type: 'file',
name: inlineAttachments[index].name,
url: restId,
isInline: true
})),
...regularRestIds.map((restId, index) => ({
type: 'file',
name: regularAttachments[index].name,
url: restId
}))
]
});
} else {
console.error("Failed to get body:", bodyResult.error.message);
}
});
Issue:
Attachments are not working correctly when type is set to 'file'. When type is set to 'item', attachments are displayed as Outlook items.
Question: How can I correctly send the email with attachments as files in the new Outlook web add-in?
