0

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?

attachemnt shown as outlook item instead of file for text document

1 Answer 1

0

So here the issue is with the inline attachments because they need type file but you are using attachments.itemId

attachments.itemId: Only used if the attachment type is set to item. The EWS item ID of the existing e-mail you want to attach to the new message. This is a string up to 100 characters.

I don't think so there is any solution if you want to use attachments.itemId then you must need to use attachments type item

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to create functionality like we did with forward the mail in Outlook?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.