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

求带.sln文件的Visual Studio版Outlook事件驱动型Office Web加载项示例

适用于Visual Studio的Outlook事件驱动Web加载项示例方案

一、创建Visual Studio兼容的Outlook Web加载项项目

  • 打开Visual Studio,创建新项目,搜索Office Add-in模板,选择带.sln的Outlook Web Add-in模板
  • 选择「邮件」类型加载项,按需跳过任务窗格选项(无需任务窗格时可选择仅事件驱动模式),生成项目结构

二、实现OnNewMessageCompose自动生成邮件主题

1. 配置Manifest文件

修改项目根目录的manifest.xml,添加事件触发声明:

<ExtensionPoint xsi:type="LaunchEvent">
  <LaunchEvents>
    <LaunchEvent Type="OnNewMessageCompose" FunctionName="generateSubject" />
  </LaunchEvents>
  <SourceLocation DefaultValue="~remoteAppUrl/FunctionFile.html" />
</ExtensionPoint>

2. 编写事件处理逻辑

在项目的FunctionFile.js(或对应脚本文件)中添加主题生成函数:

function generateSubject(event) {
  // 生成带时间戳的主题
  const autoSubject = `自动生成主题 - ${new Date().toLocaleString()}`;
  
  Office.context.mailbox.item.subject.setAsync(
    autoSubject,
    { asyncContext: event },
    (result) => {
      if (result.status !== Office.AsyncResultStatus.Succeeded) {
        console.error(`设置主题失败: ${result.error.message}`);
      }
      // 必须调用event.completed()结束事件
      event.completed();
    }
  );
}

三、实现OnMessageSend加密附件

1. 配置Manifest文件

在manifest.xml中添加发送事件声明:

<ExtensionPoint xsi:type="LaunchEvent">
  <LaunchEvents>
    <LaunchEvent Type="OnMessageSend" FunctionName="encryptAttachments" />
  </LaunchEvents>
  <SourceLocation DefaultValue="~remoteAppUrl/FunctionFile.html" />
</ExtensionPoint>

2. 编写附件加密逻辑

在脚本文件中添加附件处理函数(示例为模拟加密,需替换为真实加密实现):

function encryptAttachments(event) {
  Office.context.mailbox.item.getAttachmentsAsync(
    { asyncContext: event },
    (result) => {
      if (result.status === Office.AsyncResultStatus.Succeeded) {
        const attachments = result.value;
        if (attachments.length > 0) {
          // 此处替换为实际加密逻辑,比如调用加密API处理附件内容
          attachments.forEach(att => console.log(`正在加密附件: ${att.name}`));
        }
        // 允许邮件发送
        event.completed({ allowEvent: true });
      } else {
        console.error(`获取附件失败: ${result.error.message}`);
        // 可选:阻止发送或允许,根据业务需求调整
        event.completed({ allowEvent: false });
      }
    }
  );
}

四、调试与运行

  • 右键项目选择「启动调试」,Visual Studio会自动启动Outlook并加载当前加载项
  • 新建邮件时,OnNewMessageCompose事件自动触发并生成主题;点击发送邮件时,OnMessageSend事件触发处理附件

注意事项

  • 调试时VS会自动替换manifest.xml中的~remoteAppUrl为本地开发服务器地址,无需手动修改
  • 若需要保留任务窗格,创建项目时选择包含任务窗格的模板即可,事件驱动逻辑不受影响

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 17:07:39