如何在 outlook 插件中获取附件?

How get attachments in outlook plugin?

您好,我如何获取附件并将​​其发送到我的 java 服务器?
在文档中说:

var item = Office.context.mailbox.item;
var options = {asyncContext: {currentItem: item}};
item.getAttachmentsAsync(options, callback);

function callback(result) {
  if (result.value.length > 0) {
    for (i = 0 ; i < result.value.length ; i++) {
      result.asyncContext.currentItem.getAttachmentContentAsync(result.value[i].id, handleAttachmentsCallback);
    }
  }
}

function handleAttachmentsCallback(result) {
  // Parse string to be a url, an .eml file, a base64-encoded string, or an .icalendar file.
  switch (result.value.format) {
    case Office.MailboxEnums.AttachmentContentFormat.Base64:
      // Handle file attachment.
      break;
    case Office.MailboxEnums.AttachmentContentFormat.Eml:
      // Handle email item attachment.
      break;
    case Office.MailboxEnums.AttachmentContentFormat.ICalendar:
      // Handle .icalender attachment.
      break;
    case Office.MailboxEnums.AttachmentContentFormat.Url:
      // Handle cloud attachment.
      break;
    default:
      // Handle attachment formats that are not supported.
  }
}

但是我在这个例子中有几个错误。 首先是 item.getAttachmentsAsync is not a function
然后我尝试使用
result.asyncContext.currentItem.getAttachmentContentAsync(item.attachments[2].id, handleAttachmentsCallback);
但它从未调用过回调

我如何获取附件并通过 XMLHttpRequest 将它们发送到我的服务器?

最有可能发生的情况是您正在对已读项目尝试此代码。 getAttachmentsAsync fn 仅存在于撰写模式中,因此如果您不撰写电子邮件,您会看到上述错误。对于已读电子邮件,您应该只能访问附件 属性 Office.context.mailbox.item.attachments (https://docs.microsoft.com/en-us/javascript/api/outlook/office.attachmentdetails?view=outlook-js-preview)

阅读模式: https://docs.microsoft.com/en-us/office/dev/add-ins/outlook/get-attachments-of-an-outlook-item?view=outlook-js-preview

撰写模式: https://docs.microsoft.com/en-us/office/dev/add-ins/outlook/add-and-remove-attachments-to-an-item-in-a-compose-form?view=outlook-js-preview

但是,正如 Jadams 的回答中提到的,读取模式支持 getAttachmentContentAsync,您可以将其获取到附件的 Base64 编码中。 (第一个 link 将很快更新以反映这一点)