我正在构建一个 Outlook 插件,我需要通过 Exchange Web 服务 (EWS) 使用 Exchange On-Premises 将网络钓鱼电子邮件发送到特定的 Exchange 帐户。我正在尝试从前端向 EWS 端点发出 POST 请求,但我遇到了连接和身份验证问题。
我尝试过的:
EWS 端点:https://exchange.example.com/ews/Exchange.asmx 身份验证:我使用用户名和密码进行基本身份验证。该请求通过 POST 请求中的 XML 正文发送。 错误:我在尝试从 React 应用程序连接到服务器时遇到 ConnectTimeoutError 和获取失败错误。
const sendEmail = async () => {
const EXCHANGE_URL = "https://exchange.example.com/ews/Exchange.asmx";
const USERNAME = "[email protected]";
const PASSWORD = "password";
const xmlPayload = `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<soap:Header>
<t:RequestServerVersion Version="Exchange2016" />
</soap:Header>
<soap:Body>
<m:CreateItem xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:Items>
<t:Message>
<t:Subject>Test Email</t:Subject>
<t:Body BodyType="Text">This is a test email sent via EWS from React frontend.</t:Body>
<t:ToRecipients>
<t:Mailbox>
<t:EmailAddress>[email protected]</t:EmailAddress>
</t:Mailbox>
</t:ToRecipients>
</t:Message>
</m:Items>
</m:CreateItem>
</soap:Body>
</soap:Envelope>`;
try {
const response = await fetch(EXCHANGE_URL, {
method: "POST",
headers: {
"Content-Type": "text/xml",
"Authorization": "Basic " + btoa(`${USERNAME}:${PASSWORD}`)
},
body: xmlPayload,
});
const data = await response.text();
console.log("Email sent successfully:", data);
} catch (error) {
console.error("Error connecting to Exchange:", error);
}
};
sendEmail();
您想要构建什么类型的插件?如果它是一个基于 Com+ 的旧插件,那么不要使用 EWS,最好只使用 OOM 来发送它。使用较新的插件框架,您通常会使用回调令牌 https://learn.microsoft.com/en-gb/office/dev/add-ins/outlook/authentication?redirectedfrom=MSDN#callback-tokens 然后使用makeEwsRequestAsync https://learn.microsoft.com/en-us/office/dev/add-ins/outlook/web-services 这意味着身份验证和路由/发现都为您处理。
您可以使用 EWSeditor 开始测试 EWS https://github.com/dseph/EwsEditor/releases,它允许您在编写的任何代码之外测试身份验证和连接性。