如何在点击Outlook新邮件发送按钮后触发自定义Outlook加载项
实现Outlook新邮件发送后触发自定义加载项的方法
要在点击新邮件发送按钮后触发或打开你的自定义Outlook加载项,可通过以下步骤实现:
1. 利用Outlook JavaScript API监听发送事件
Outlook加载项支持监听邮件的ItemSend事件,你可以在加载项代码中注册该事件,在用户点击发送时触发自定义逻辑,甚至直接弹出加载项面板。
核心代码示例
在加载项的JavaScript文件中添加以下逻辑:
Office.initialize = function () { // 注册ItemSend事件 Office.context.mailbox.addHandlerAsync(Office.EventType.ItemSend, onSendHandler, function (result) { if (result.status === Office.AsyncResultStatus.Failed) { console.error("注册发送事件失败: " + result.error.message); } }); }; function onSendHandler(eventArgs) { // 触发自定义加载项面板 Office.context.ui.displayDialogAsync( "你的加载项面板地址.html", { height: 50, width: 50 }, function (asyncResult) { if (asyncResult.status === Office.AsyncResultStatus.Failed) { console.error("打开加载项面板失败: " + asyncResult.error.message); // 打开失败时允许邮件正常发送 eventArgs.completed({ allowEvent: true }); } else { // 监听面板操作结果,再决定是否允许发送邮件 const dialog = asyncResult.value; dialog.addEventHandler(Office.EventType.DialogMessageReceived, function (args) { const allowSend = args.message === "allow"; eventArgs.completed({ allowEvent: allowSend }); dialog.close(); }); } } ); }
2. 配置加载项清单文件
确保加载项清单(manifest.xml)中声明对ItemSend事件的支持,同时开启邮件撰写场景的加载项权限:
<VersionOverrides xmlns="http://schemas.microsoft.com/office/mailappversionoverrides" xsi:type="VersionOverridesV1_0"> <Requirements> <bt:Sets DefaultMinVersion="1.1"> <bt:Set Name="Mailbox" /> </bt:Sets> </Requirements> <Hosts> <Host xsi:type="MailHost"> <DesktopFormFactor> <FunctionFile resid="Commands.Url" /> <!-- 确保加载项在撰写场景可见 --> <ExtensionPoint xsi:type="MessageComposeCommandSurface"> <OfficeTab id="TabDefault"> <Group id="ComposeGroup"> <Label resid="GroupLabel" /> <Control xsi:type="Button" id="ComposeButton"> <Label resid="ButtonLabel" /> <Supertip> <Title resid="ButtonTitle" /> <Description resid="ButtonDesc" /> </Supertip> <Icon> <bt:Image size="16" resid="Icon.16x16" /> <bt:Image size="32" resid="Icon.32x32" /> <bt:Image size="80" resid="Icon.80x80" /> </Icon> <Action xsi:type="ShowTaskpane"> <SourceLocation resid="Taskpane.Url" /> </Action> </Control> </Group> </OfficeTab> </ExtensionPoint> <!-- 声明支持ItemSend事件 --> <ExtensionPoint xsi:type="Events"> <Event Type="ItemSend" FunctionExecution="synchronous" FunctionName="onSendHandler" /> </ExtensionPoint> </DesktopFormFactor> </Host> </Hosts> <!-- 其他清单配置 --> </VersionOverrides>
3. 关键注意事项
- 必须确保加载项在邮件撰写模式下已启用,否则无法监听
ItemSend事件。 ItemSend事件支持同步/异步处理,若需等待用户在加载项面板的操作,需将FunctionExecution设为synchronous,并通过eventArgs.completed()决定是否允许邮件发送。- 测试时优先使用Outlook桌面客户端或网页版,部分旧版本客户端可能不支持该事件。
内容的提问来源于stack exchange,提问作者R. Mohammad
相关产品推荐
相关产品推荐

