You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Outlook Add-in API获取邮件主题时遇getAsync未定义错误求助

Outlook加载项获取邮件主题报错:Office.context.mailbox.item.subject.getAsync 不是函数

问题背景

  • 需求:通过Outlook Add-in API从收件箱获取邮件主题
  • 环境:
    • 侧载的Outlook加载项
    • 清单文件中Mailbox MinVersion设为1.1
    • 开发工具:Visual Studio Code

报错代码

async function ReadEmailSubject() {
  document.getElementById("email-subject").innerText = ""; 
  Office.context.mailbox.item.subject.getAsync(function (result) {
    var emailSubjectDiv = document.getElementById("email-subject");
    if (result.status === Office.AsyncResultStatus.Succeeded) {
        var emailSubject = result.value;
        emailSubjectDiv.innerText = emailSubject;
    } else {
        console.error("Failed to get email subject: " + result.error.message);
        emailSubjectDiv.innerText = "Failed to get email body: " + result.error.message;
    }
  });
}

错误信息

TypeError: Office.context.mailbox.item.subject.getAsync is not a function
    at _callee2$ (e:\office-add-ins\Outlook\yo-main\Outlook Add-in\src\taskpane\taskpane.js:28:39)
    at tryCatch (e:\office-add-ins\Outlook\yo-main\Outlook Add-in\src\taskpane\taskpane.js:2:1)
    at Generator.<anonymous> (e:\office-add-ins\Outlook\yo-main\Outlook Add-in\src\taskpane\taskpane.js:2:1)
    at Generator.next (e:\office-add-ins\Outlook\yo-main\Outlook Add-in\src\taskpane\taskpane.js:2:1)
    at asyncGeneratorStep (e:\office-add-ins\Outlook\yo-main\Outlook Add-in\src\taskpane\taskpane.js:2:1)
    at _next (e:\office-add-ins\Outlook\yo-main\Outlook Add-in\src\taskpane\taskpane.js:2:1)
    at e:\office-add-ins\Outlook\yo-main\Outlook Add-in\src\taskpane\taskpane.js:2:1
    at new Promise (<anonymous>)
    at e:\office-add-ins\Outlook\yo-main\Outlook Add-in\src\taskpane\taskpane.js:2:1
    at _ReadEmailSubject (https://localhost:3000/taskpane.js:266:28) {stack: 'TypeError: Office.context.mailbox.item.subjec…t (https://localhost:3000/taskpane.js:266:28)', message: 'Office.context.mailbox.item.subject.getAsync is not a function'}

已尝试操作

  • 修改清单文件中Mailbox MinVersion为1.1
  • 给getAsync添加asyncContext参数,错误仍存在

解决方案

问题出在API版本的兼容性上:
在Mailbox API 1.1版本中,Office.context.mailbox.item.subject是一个直接可访问的字符串属性,不需要调用getAsync异步方法。getAsync是后续版本(如1.3及以上)才引入的异步获取属性的方式,用于处理那些可能需要加载的复杂属性,但subject在早期版本是同步返回的。

修正后的代码如下:

async function ReadEmailSubject() {
  document.getElementById("email-subject").innerText = "";
  var emailSubjectDiv = document.getElementById("email-subject");
  try {
    // 直接访问subject属性,无需getAsync
    var emailSubject = Office.context.mailbox.item.subject;
    emailSubjectDiv.innerText = emailSubject;
  } catch (error) {
    console.error("Failed to get email subject: " + error.message);
    emailSubjectDiv.innerText = "获取邮件主题失败: " + error.message;
  }
}

如果后续需要使用更高版本的API特性(比如异步获取其他属性),可以将清单文件中的Mailbox MinVersion升级到1.3或更高,但要注意对应Outlook客户端的兼容性。


内容的提问来源于stack exchange,提问作者Ivory

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 08:29:58