Forum Discussion
please help with : The response received from the service didn't contain valid XM error
New Error
Message Error: The request failed. The remote server returned an error: (401) Unauthorized.
It looks like the issue is in part 3 of the code?
Please can you confirm where in the code is the senders email?
Hi OdeneGerber,
thanks for your update,
It seems that the issue is still related to authentication, and the error "(401) Unauthorized" suggests that the credentials provided in your code are not being accepted by the server.
Let's review the code, particularly the section related to credentials:
try
{
service.Credentials = new WebCredentials("****email address removed for privacy reasons****", "****userpassword****");
}
catch (Exception ex)
{
return BadRequest("Credentials: " + ex.Message);
}
In this code block, you are setting the credentials for the `ExchangeService` using a username and password.
Ensure that:
1. The email address and password provided are correct.
2. The account associated with the provided email address has the necessary permissions to send emails via EWS.
If the email address and password are correct, you may need to review the permissions and configuration on the Office 365 side. Here are a few steps to consider:
1. Application Impersonation Permissions: Make sure that the account used in the credentials has ApplicationImpersonation permissions. This is necessary for sending emails on behalf of another user.
2. Check Account Permissions: Verify that the account has the required permissions to send emails using EWS. You might need to check the Exchange Online settings to grant the necessary permissions.
3. Use Autodiscover: As previously mentioned, consider using Autodiscover to automatically discover the correct EWS endpoint URL. Replace the hardcoded URL with:
service.AutodiscoverUrl("****email address removed for privacy reasons****");
Make sure to update your code accordingly. Using Autodiscover can help ensure that your application is always using the correct EWS endpoint.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)