I am currently developing an Outlook add-in designed to send phishing and spam reports using the makeEwsRequestAsync method. However, I am encountering a persistent error: ‘The remote server returned an error: (403) Forbidden.’ This error suggests that my request to the Exchange Web Services (EWS) is being denied, but I am unsure of the specific cause. I have checked the permissions of the user account being used, verified the EWS endpoint URL, and ensured that the authentication method is correctly implemented. Despite these efforts, the error persists. What additional steps can I take to troubleshoot and resolve this issue?
this is my code
Office.onReady(() => {
document.getElementById("sendMailButton").onclick = sendEmail;
});
function createSendItemRequest(toRecipients, subject, body) {
// Trim to remove leading whitespace/newlines
const request = `<?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"
xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages">
<soap:Header>
<t:RequestServerVersion Version="Exchange2013" />
</soap:Header>
<soap:Body>
<m:CreateItem MessageDisposition="SendAndSaveCopy">
<m:Items>
<t:Message>
<t:Subject>${subject}</t:Subject>
<t:Body BodyType="Text">${body}</t:Body>
<t:ToRecipients>
${toRecipients.map(email => `<t:Mailbox><t:EmailAddress>${email}</t:EmailAddress></t:Mailbox>`).join('')}
</t:ToRecipients>
</t:Message>
</m:Items>
</m:CreateItem>
</soap:Body>
</soap:Envelope>`;
return request.trim(); // Trim any leading/trailing whitespace
}
function sendEmail() {
const mailbox = Office.context.mailbox;
// Define the recipients, subject, and body of the email
const toRecipients = ["[email protected]"];
const subject = "Test Email from Outlook Add-in";
const body = "Hello, this is a test email sent via the Outlook add-in using EWS.";
// Create the SOAP request
const soapRequest = createSendItemRequest(toRecipients, subject, body);
// Call the makeEwsRequestAsync method to send the email
mailbox.makeEwsRequestAsync(soapRequest, callback);
}
function callback(asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
console.log("Email sent successfully!");
} else {
console.error("Failed to send email: ", asyncResult.error.message);
}
}