Reputation: 29
I am building a outlook addin where I need to send an email for phishing to specific exchange account using Exchange On-Premises via Exchange Web Services (EWS). I am trying to make a POST request to the EWS endpoint from the frontend, but I’m facing issues with connectivity and authentication.
What I’ve tried:
EWS Endpoint: https://exchange.example.com/ews/Exchange.asmx Authentication: I am using basic authentication with a username and password. The request is sent with an XML body in the POST request. Error: I’m encountering a ConnectTimeoutError and fetch failed error while trying to connect to the server from the React app.
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();
Upvotes: 0
Views: 64
Reputation: 22032
What type of addin are you trying to build ? if its an old Com+ based addin then don't use EWS you better of just using the OOM to send it. With the newer addin framework you would normally use a Callback tokens https://learn.microsoft.com/en-gb/office/dev/add-ins/outlook/authentication?redirectedfrom=MSDN#callback-tokens and then use makeEwsRequestAsync https://learn.microsoft.com/en-us/office/dev/add-ins/outlook/web-services this means the Auth and routing/discovery are all handled for you.
You could start with testing EWS using the EWSeditor https://github.com/dseph/EwsEditor/releases that allows you to test the authentication and connectivity outside of any code your writing.
Upvotes: 0