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

Outlook网页版插件:如何在回复/转发撰写页获取原邮件ID

获取Outlook网页版插件回复/转发时的原邮件Item ID

一、通过Office JS+Graph API精准获取(推荐)

在回复/转发触发的messageCompose函数中,可通过以下步骤拿到原邮件的Item ID:

  1. 获取原邮件的InternetMessageId
    撰写窗口的inReplyTo属性直接对应被回复/转发的原邮件的互联网消息ID,通过异步方法获取:
Office.context.mailbox.item.inReplyTo.getAsync(function(result) {
  if (result.status === Office.AsyncResultStatus.Succeeded) {
    const originalInternetMsgId = result.value;
    fetchOriginalItemId(originalInternetMsgId);
  }
});
  1. 调用Graph API转换为Item ID
    先获取Graph访问令牌,再通过internetMessageId过滤查询原邮件,拿到对应的id(即Item ID):
function fetchOriginalItemId(internetMsgId) {
  Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function(tokenResult) {
    if (tokenResult.status === Office.AsyncResultStatus.Succeeded) {
      const accessToken = tokenResult.value;
      const encodedId = encodeURIComponent(internetMsgId);
      fetch(`https://graph.microsoft.com/v1.0/me/messages?$filter=internetMessageId eq '${encodedId}'`, {
        headers: { 'Authorization': `Bearer ${accessToken}` }
      })
      .then(res => res.json())
      .then(data => {
        if (data.value.length > 0) {
          const originalItemId = data.value[0].id;
          // 拿到目标ID后执行后续逻辑
          console.log('原邮件Item ID:', originalItemId);
        }
      });
    }
  });
}

注意事项

  • 插件需在manifest.xml中声明Mail.Read权限,否则无法调用Graph API读取邮件数据。
  • internetMessageId是全局唯一标识单封邮件的字段,不会和线程内其他邮件混淆,比conversationId更精准。

二、阅读模式缓存原邮件ID(有局限性)

如果用户会先打开原邮件阅读再操作回复/转发,可以提前缓存原邮件ID:

  1. 注册阅读模式事件,缓存ID
    在插件初始化时注册OnMessageRead事件,获取当前阅读邮件的Item ID并存入自定义属性或本地存储:
function initPlugin() {
  Office.context.mailbox.addHandlerAsync(Office.EventType.ItemRead, messageReadHandler);
}

function messageReadHandler() {
  Office.context.mailbox.item.itemId.getAsync(function(result) {
    if (result.status === Office.AsyncResultStatus.Succeeded) {
      const originalItemId = result.value;
      // 存入自定义属性
      Office.context.mailbox.item.loadCustomPropertiesAsync(function(propsResult) {
        const customProps = propsResult.value;
        customProps.set('cachedOriginalItemId', originalItemId);
        customProps.saveAsync();
      });
      // 或存入localStorage(注意跨窗口同步问题)
      localStorage.setItem('cachedOriginalItemId', originalItemId);
    }
  });
}
  1. 在撰写模式读取缓存ID
    在messageCompose函数中读取之前缓存的ID:
function messageCompose() {
  // 从自定义属性读取
  Office.context.mailbox.item.loadCustomPropertiesAsync(function(propsResult) {
    const customProps = propsResult.value;
    const originalItemId = customProps.get('cachedOriginalItemId');
    if (originalItemId) {
      // 使用ID进行后续操作
    }
  });
}

局限性

如果用户直接从收件箱选中邮件点击回复/转发,未触发阅读模式,缓存不会有数据,因此仅适用于特定操作流程。

内容的提问来源于stack exchange,提问作者Md. Mohibur Rahman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 10:46:16