咨询:Outlook插件服务器宕机时如何避免邮件无法发送
Outlook Add-in 服务器宕机时无法发送邮件的解决方案
问题背景
我们开发的Outlook Add-in正常运行无异常,但当承载Add-in代码的服务器宕机时,用户无法发送邮件,且收到错误提示:
"Outlook can't send this message because there's a problem with the add-in [Add-in name]"
我们已尝试过相关主题的解决方案及官方策略调整,但问题仍未解决,现需实现服务器宕机时用户仍能正常发送邮件的目标。
当前Manifest配置
<OfficeApp xmlns="http://schemas.microsoft.com/office/appforoffice/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bt="http://schemas.microsoft.com/office/officeappbasictypes/1.0" xmlns:mailappor="http://schemas.microsoft.com/office/mailappversionoverrides/1.0" xsi:type="MailApp"> <Id>some guid</Id> <Version>1.0.0.0</Version> <ProviderName>Test ProviderName</ProviderName> <DefaultLocale>da-DK</DefaultLocale> <DisplayName DefaultValue="Add-in name"/> <Description DefaultValue="Test description"/> <IconUrl DefaultValue="https://localhost:3000/assets/icon-64.png"/> <HighResolutionIconUrl DefaultValue="https://localhost:3000/assets/icon-128.png"/> <SupportUrl DefaultValue="https://localhost:3000/help"/> <AppDomains> <!-- All domains you want to access must be listed here (origin) --> <AppDomain>https://localhost:3000/</AppDomain> <AppDomain>https://localhost:5001/</AppDomain> </AppDomains> <Hosts> <Host Name="Mailbox"/> </Hosts> <Requirements> <Sets> <Set Name="Mailbox" MinVersion="1.1"/> </Sets> </Requirements> <!-- Old schema. This will be overridden by the VersionOverrides below. --> <FormSettings> <Form xsi:type="ItemEdit"> <DesktopSettings> <SourceLocation DefaultValue="https://localhost:3000/commands.html"/> </DesktopSettings> </Form> </FormSettings> <!-- /Old schema. --> <Permissions>ReadWriteMailbox</Permissions> <Rule xsi:type="RuleCollection" Mode="Or"> <Rule xsi:type="ItemIs" ItemType="Message" FormType="Edit" /> </Rule> <VersionOverrides xmlns="http://schemas.microsoft.com/office/mailappversionoverrides" xsi:type="VersionOverridesV1_0"> <!-- On Send requires VersionOverridesV1_1 --> <VersionOverrides xmlns="http://schemas.microsoft.com/office/mailappversionoverrides/1.1" xsi:type="VersionOverridesV1_1"> <Description resid="residAppDescription" /> <Requirements> <bt:Sets DefaultMinVersion="1.3"> <bt:Set Name="Mailbox" /> </bt:Sets> </Requirements> <Hosts> <Host xsi:type="MailHost"> <!-- Event-based activation happens in a lightweight runtime.--> <Runtimes> <!-- HTML file including reference to or inline JavaScript event handlers. This is used by Outlook on the web and Outlook on the new Mac UI. --> <Runtime resid="WebViewRuntime.Url"> <!-- JavaScript file containing event handlers. This is used by Outlook on Windows. --> <Override type="javascript" resid="JSRuntime.Url"/> </Runtime> </Runtimes> <DesktopFormFactor> <FunctionFile resid="WebViewRuntime.Url" /> <ExtensionPoint xsi:type="Events"> <!-- Using ItemSend allows us to open dialogs from the send event, as opposed to when using the "Smart-Alert" on send handler. The draw-back of this is that we cannot publish this add-in to Microsoft's AppSource. The function must be on the global scope. --> <Event Type="ItemSend" FunctionExecution="synchronous" FunctionName="onMessageSendHandler" /> </ExtensionPoint> <ExtensionPoint xsi:type="LaunchEvent"> <LaunchEvents> <LaunchEvent Type="OnMessageCompose" FunctionName="onMessageComposeHandler"/> <LaunchEvent Type="OnMessageAttachmentsChanged" FunctionName="onMessageAttachmentsChangedHandler"/> </LaunchEvents> <SourceLocation resid="WebViewRuntime.Url"/> </ExtensionPoint> </DesktopFormFactor> </Host> </Hosts> <Resources> <bt:Urls> <!-- The JavaScript code is hosted on a secure and trusted web server. --> <bt:Url id="WebViewRuntime.Url" DefaultValue="https://localhost:3000/commands.html"/> <bt:Url id="JSRuntime.Url" DefaultValue="https://localhost:3000/commands.js" /> </bt:Urls> </Resources> </VersionOverrides> </VersionOverrides> </OfficeApp>
解决方案
问题根源
当前配置中ItemSend事件使用了同步执行模式(FunctionExecution="synchronous"),这种模式下Outlook会阻塞邮件发送流程,等待Add-in的处理结果。如果服务器宕机导致脚本无法加载或接口调用失败,Outlook会直接判定Add-in异常,阻止邮件发送。
具体修复步骤
1. 切换为异步事件处理(优先推荐)
将ItemSend事件的执行模式改为异步,这样Outlook不会等待Add-in处理完成,即使服务器不可用,邮件也能正常发出。
修改Manifest中的Event节点:
<Event Type="ItemSend" FunctionExecution="async" FunctionName="onMessageSendHandler" />
同时调整onMessageSendHandler函数,通过Promise处理异步逻辑,服务器不可用时直接允许发送:
async function onMessageSendHandler(event) { try { // 原有调用远程服务的逻辑 await fetch('你的业务接口地址'); // 服务正常时的处理逻辑 event.completed({ allowEvent: true }); } catch (error) { // 服务器不可用/调用失败时,直接放行邮件 event.completed({ allowEvent: true }); } }
2. 同步模式下的超时容错(若必须保留同步)
如果因为业务需求必须使用同步模式(例如需要在发送前修改邮件内容),则在代码中添加超时逻辑,避免长时间等待服务器响应:
function onMessageSendHandler(event) { // 调用远程服务 const serviceRequest = fetch('你的业务接口地址'); // 设置5秒超时 const timeoutPromise = new Promise(resolve => setTimeout(() => resolve(true), 5000)); // 优先取先完成的结果 Promise.race([serviceRequest, timeoutPromise]) .then(() => event.completed({ allowEvent: true })) .catch(() => event.completed({ allowEvent: true })); }
3. 确认Manifest的兼容性
当前Manifest中Mailbox Set的最低版本为1.3,已经支持异步ItemSend事件,无需额外调整版本要求。
关键注意事项
- 异步模式下无法直接修改邮件内容,若需修改需使用Smart Alert模式(但该模式无法发布到AppSource,与你当前Manifest注释中的说明一致)
- 无论同步还是异步模式,处理函数必须调用
event.completed(),否则Outlook会一直等待导致发送失败 - 服务器异常时,必须明确返回
allowEvent: true,确保Outlook继续执行发送流程
内容的提问来源于stack exchange,提问作者Alexander Byrka
相关产品推荐
相关产品推荐

