please help with : The response received from the service didn't contain valid XM error

Copper Contributor

Good day Hope someone can assist met with the following problem.

We are trying to setup a scenario where out application running on windows server 2019 iis goes and login with the following email address username and password specified and automatically send an mail back to the client from office 365.

In simple terms the app must open web url login with credentials given and send mail.

I went through a lot of the forms already and tried to change the URL quick a few times since some of the forms aren't consisted what to actually use here.

So what needs to happen application needs to contact office 365 on url (https://outlook.office365.com/EWS/Exchange.asmx) as per previous forms stated but then we get the following error "The error message you're encountering, "(401) Unauthorized"

We have given permission on the noreply/order email address on 365 and double checked username and password but error message still persists we also made sure 2 factor auth is off on 365 admin center for this mail box to sent out and delegations are correct.. WE also used https://autodiscover.outlook.com/EWS/Exchange.asmx since in the admin portal of 365 it seems that everything is set to autodiscover but with no success.

We then changed the url to "https://outlook.office.com/mail/ourdomain.co.za/EWS/Exchange.asmx" as per other forms, you get the office 365 login page which seems to be the correct url but now we are getting this error "The response received from the service didn't contain valid XML"

We are not running an hybrid solution and only want to use the application to send via office 365 we do not have an on site exchange server. Firewalls rules were also checked and cant seem to find anything blocking this requests.

Also I am not to sure if there is anything I need to do on our windows server iis side the only thing i tried there was to add and allow the url under sites in iis request filtering which also did not make any difference.

Please see current code of application. Areas in **** we have the actual details filled in.


ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);

//Set your Exchange server URL and credentials.
try
{
// Set the correct URL for Office 365.
service.Url = new Uri("https://outlook.office.com/mail/ourdomain.co.za/EWS/Exchange.asmx");
}
catch (Exception ex)
{
return BadRequest("WebService Uri: " + ex.Message);
}


try
{
service.Credentials = new WebCredentials("****email address removed for privacy reasons****", "****userpassword****");
}
catch (Exception ex)
{
return BadRequest("Credentials: " + ex.Message);
}

try
{
EmailMessage email = new EmailMessage(service);
email.Subject = "Your Visits for today";
email.Body = new MessageBody("The email is working");
email.ToRecipients.Add("****email address removed for privacy reasons****");
email.Send();

return Ok("Email sent");
}
catch (Exception ex)
{
return BadRequest("Message Error: " + ex.Message);
     }
        }

 

Please any help or assistance will help greatly since we are currently at this point we do not know what to check further.

3 Replies

Hi @Eddy-2023,

The error message you're encountering, "(401) Unauthorized," indicates that there is an issue with the authentication and authorization when your application is trying to access Office 365 services.

The "The response received from the service didn't contain valid XML" error suggests there might also be a problem with the response from the server.

Here are some steps to help you troubleshoot and resolve the issue:

1. Check Office 365 Credentials:

  • Ensure that the email address and password you are using in your code are correct and have the necessary permissions to send emails on behalf of the specified account.

2. Use Autodiscover:

  • Instead of hardcoding the URL, you can use Autodiscover to automatically discover the correct EWS endpoint URL. This is a more flexible and recommended approach.
 

 

service.AutodiscoverUrl("****email address removed for privacy reasons****");

 

Make sure your code uses the correct Autodiscover method.

3. Service Permissions:

  • Verify that the account you're using to authenticate has the necessary permissions to access and send emails through the EWS. You might need to grant ApplicationImpersonation permissions.

4. Check if 2FA is disabled:

  • Ensure that two-factor authentication (2FA) is disabled for the account you are using in your application. You mentioned that you've turned it off, but double-check to confirm.

5. Delegations and Permissions:

  • Ensure that the permissions and delegations are set up correctly in Office 365 for the sending account.

6. URL and Endpoints:

7. Firewall and Network Issues:

  • Make sure that there are no firewall or network-related issues that might be blocking outgoing requests from your server to Office 365 services. Verify that the server can reach the necessary endpoints.

8. Server Configuration:

  • Check your Windows Server IIS configuration to ensure it's not blocking outbound requests to the Office 365 service. Make sure that the URL you are trying to access is not restricted by any URL filtering or proxy settings.


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)

@LeonPavesic 

 

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)