Connection to Office 365 by EWS API

39,852

Solution 1

You can use the code below to connect to the EWS on office 365:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);

service.Credentials = new WebCredentials("[email protected]", "password");
service.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback);

You need define one callback function for the AutodiscoveryUrl function, like this:

private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
    // The default for the validation callback is to reject the URL.
    bool result = false;

    Uri redirectionUri = new Uri(redirectionUrl);

    // Validate the contents of the redirection URL. In this simple validation
    // callback, the redirection URL is considered valid if it is using HTTPS
    // to encrypt the authentication credentials. 
    if (redirectionUri.Scheme == "https")
    {
        result = true;
    }
    return result;
}

Solution 2

There appears 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")
};

Importat 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 exeption.

Solution 3

You cannot use basic authentication (username and password) in your EWS application to connect to Office/Microsoft 365 now. Microsoft no longer supports basic authentication in EWS for Exchange Online. You are advised to use OAuth2.0 to get a token and use the same in your EWS client to connect to Office 365.

For this you will have to register your application in Azure AD to use client credential flow. I did a POC on this using console application. enter image description here

enter image description here

Here's the link to GitHub repository for source code and documentation.

Solution 4

I know this is a fairly old solution, but it was still very helpful to me. I have a few tools that worked with the "normal" network version of Exchange, but so far my tests with Exchange Online failed (i got errors like "The Autodiscover service couldn't be located", etc).

Essential here is to use WebCredentials instead of NetworkCredential and a e-mailaddress instead of a username.

Share:
39,852

Related videos on Youtube

Muflix
Author by

Muflix

Updated on September 09, 2021

Comments

  • Muflix
    Muflix over 2 years

    I am using EWS API in my console application to process mailbox items and my connection script looks like

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
    service.UseDefaultCredentials = true;
    service.AutodiscoverUrl("[email protected]");
    

    But i found that my email account was moved to Office 365 cloud. How should i change the authentication ?

    i found EWS service url

     service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
    

    but i dont know how to use it.

    Thank you

    • kat0r
      kat0r over 8 years
      You very likely only have to set your credentials (username/pw) in service.Credentials.
    • Chris
      Chris about 8 years
      I voted your question up since your question was the answer to my question. In my case autodiscover did not work remotly only on-premises, but as soon as I put service.Url = new Uri("outlook.office365.com/EWS/Exchange.asmx"); explicitly instead of autodiscover it worked like a charm Thank you very much again
    • wruckie
      wruckie almost 7 years
      AutoDiscover was very slow, but setting the URI worked much better
  • Muflix
    Muflix over 8 years
    It works, thank you ! But do you also know how to use windows authentication ? because i cant store password in the code. I found this article jeremythake.com/2014/08/… but i dont know what is ServiceResourceId variable.
  • Matt
    Matt over 8 years
    @Muflix, it is OAuth, not Windows authentication. Please refer this article:msdn.microsoft.com/en-us/library/office/…
  • Dung
    Dung almost 8 years
    to be able to select the right version of Exchange service such as (changeService(ExchangeVersion.Exchange2013_SP1)) you need to have the right version of "Microsoft.Exchange.WebServices.dll". This cause a lot of headache for me, I hope it helps others.
  • Alex
    Alex over 7 years
    Is this still working? For me, Autodiscover function against outlook.com and office365.com domains results in various errors (302, 401, host not found). For instance, with tracing enabled, I see it tries to get something at autodiscover.office365.com/autodiscover/autodiscover.xml location (which seems to be the right place) but nobody's there and I get "The remote name could not be resolved".
  • Alex
    Alex over 7 years
    OK, got it working for at least one particular account (hosted at office365.com but having its own domain).
  • IbrarMumtaz
    IbrarMumtaz over 6 years
    @Matt, any idea on how to retrieve/browse mailboxes on an office 365 account?
  • PerlDev
    PerlDev almost 6 years
    I tried exact code, but got error "The Autodiscover service couldn't be located".
  • Dave
    Dave almost 6 years
    @PerlDev - Did you ever resolve your issue? I am having the exact same problem .
  • PerlDev
    PerlDev over 5 years
    The following code works for me: _service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); _service.UseDefaultCredentials = true; _service.AutodiscoverUrl(appUserEmail);
  • Praveen Nayak
    Praveen Nayak over 3 years
    Based on MSDN documentation, this appears to be basic authentication usage and not Oauth. OAuth would need Azure AD registration for the app, acquiring token in an app-only mode and using that token for auth - docs.microsoft.com/en-us/exchange/client-developer/…
  • Muflix
    Muflix over 3 years
    Thank you, great documentation :-)
  • DirectionUnkown
    DirectionUnkown over 2 years
  • santhoshraj
    santhoshraj about 2 years
    So we should register our application in our Azure ID's in which the mail box has access to send the mail?