When using the Test Connection option from an Email Server Profile in Dynamics 365, the test fails and you may see an error like the following example:
"Remote server error: (401) Unauthorized. Make sure the user name and password you entered are correct and try again."
"System.Net.WebException: The operation has timed out
at Microsoft.Crm.Asynchronous.EmailConnector.ExchangeConnectivityDiscoverer.ValidateServerUrl(EmailDirectionCode emailDirection)"
Cause 1: The username or password are not correct.
Cause 2: If you are using Dynamics 365 (online) to connect to Exchange on-premises, this error can occur if your firewall does not allow connectivity from Dynamics 365 to your Exchange server.
Cause 3: In some cases, the test connection may fail even though the credentials are correct and connectivity is not a problem. See Resolution 3 in the Resolution section.
Resolution 1: Verify the username and password are correct.
NOTE: The User Name value needs to be in UPN format. Ex. email@example.com instead of contoso\username.
Resolution 2: If you are using Dynamics 365 (online) with Exchange on-premises, verify your firewall allows connectivity from the list of IP ranges documented here including the referenced Azure IP ranges:
Resolution 3: The Test Connection results are not always reliable. If the same username and password works when performing a test connection from https://testconnectivity.microsoft.com, use the Test & Enable Mailboxes option in Dynamics 365 instead of the Test Connection option. If the Test & Enable is successful for your mailboxes, you can disregard the results from the Test Connection dialog.
NOTE: If any failures occur using the Test & Enable Mailboxes button, see the Alerts tab within the mailbox record and review any help links that are included.