当您在 Outlook 中单击“回复”时,它不会保留附件,因此我正在开发一个 Web 插件“带附件回复” - 单击我的插件按钮后,应打开一个新的回复窗口,并且所选邮件项目的附件应附加到该回复窗口。 根据 Microsoft 文档,这是显示带有附件的回复表单的方式:
Office.context.mailbox.item.displayReplyForm(
{
'htmlBody' : 'hi',
'attachments' :
[
{
'type' : Office.MailboxEnums.AttachmentType.File,
'name' : 'squirrel.png',
'url' : 'http://i.imgur.com/sRgTlGR.jpg'
}
]
});
但是是否可以通过指定 URL 以外的方式将附件添加到回复表单中?我已经在代码中包含了当前电子邮件的附件:
let attachments = Office.context.mailbox.item.attachments;
我想到的一种方法是将附件上传到文件存储系统(Egnyte),然后在附件的“url”属性中指定每个文件的URL,但随后我必须暂时将这些文件公开,然后删除它们将它们添加到回复表单后。但我不想公开敏感文件,即使是暂时的。而且对于如此简单直接的事情来说,这种方法似乎有点矫枉过正。 有什么想法吗?
是的,可以在回复表单中添加附件,而无需指定 URL。您可以使用
Office.context.mailbox.item.addFileAttachmentAsync
方法将文件附件添加到回复表单中。
这里有一个示例代码片段,演示如何使用此方法将当前电子邮件的所有附件添加到回复表单中:
Office.context.mailbox.item.displayReplyForm(
{
htmlBody: 'hi',
},
async (result) => {
if (result.status === Office.AsyncResultStatus.Succeeded) {
const replyItem = result.value;
const attachments = Office.context.mailbox.item.attachments;
for (let i = 0; i < attachments.length; i++) {
const attachment = attachments[i];
const attachmentContent = await attachment.getAsync(Office.CoercionType.Base64);
replyItem.addFileAttachmentAsync(
attachment.name,
attachmentContent.value,
{ asyncContext: null },
(addAttachmentResult) => {
if (addAttachmentResult.status !== Office.AsyncResultStatus.Succeeded) {
console.error(`Failed to add attachment: ${addAttachmentResult.error.message}`);
}
}
);
}
} else {
console.error(`Failed to display reply form: ${result.error.message}`);
}
}
);
此代码使用
Office.context.mailbox.item.displayReplyForm
方法来显示回复表单。然后,它使用 Office.context.mailbox.item.attachments
属性检索当前电子邮件的附件。
对于每个附件,它使用
attachment.getAsync
方法将附件内容检索为 Base64 编码的字符串。然后,它使用 replyItem.addFileAttachmentAsync
方法将附件添加到回复表单中。
请注意,
addFileAttachmentAsync
方法将附件名称和内容作为参数,以及可选的options
参数,可用于指定其他选项,例如asyncContext
。
此方法不需要您将附件上传到文件存储系统或将其公开,并允许您将附件直接添加到回复表单中。