There appear to have been a few changes in EWS connection to office365 in regards to security, causing Matt's answer to not work for me.
What did work is the following:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1)
{
Credentials = new WebCredentials("user", "password", "domain"),
Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx")
};
Important notes:
AutodiscoverUrl
did not complete successfully, it failed to discover the URL every time
user
must be the full email address of the user.
domain
is the NetBIOS domain name, meaning it is only the domain name.
You can find the domain in the Microsoft 365 admin center under Settings -> Domains
. The easiest way is to find the fallback domain in the form of domain.onmicrosoft.com
. Take only the domain
part.
The user must be a Microsoft 365 admin
Any change in one of these parameters caused an Unauthorized
exception.
EDIT
Basic authentication is no longer supported by Microsoft.
Follow this article on how to connect to EWS via OAuth.
Worked on the very first try with no issues.