28

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("emailService@domain.com");

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

Muflix
  • 6,192
  • 17
  • 77
  • 153
  • You very likely only have to set your credentials (username/pw) in `service.Credentials`. – kat0r Sep 02 '15 at 14:17
  • 8
    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("https://outlook.office365.com/EWS/Exchange.asmx"); explicitly instead of autodiscover it worked like a charm Thank you very much again – Chris Mar 29 '16 at 16:55
  • AutoDiscover was very slow, but setting the URI worked much better – wruckie May 23 '17 at 16:04

4 Answers4

23

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("emailService@domain.com", "password");
service.AutodiscoverUrl("emailService@domain.com", 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;
}
ataravati
  • 8,891
  • 9
  • 57
  • 89
Matt
  • 6,010
  • 25
  • 36
  • 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 http://www.jeremythake.com/2014/08/using-the-exchange-online-ews-api-with-office-365-api-via-azure-ad/ but i dont know what is ServiceResourceId variable. – Muflix Sep 03 '15 at 07:58
  • 1
    @Muflix, it is OAuth, not Windows authentication. Please refer this article:https://msdn.microsoft.com/en-us/library/office/dn626019(v=exchg.150).aspx#sectionSection1 – Matt Sep 03 '15 at 15:41
  • 2
    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. – Dung May 19 '16 at 17:52
  • 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 http://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 Oct 12 '16 at 15:54
  • OK, got it working for at least one particular account (hosted at office365.com but having its own domain). – Alex Oct 12 '16 at 16:32
  • @Matt, any idea on how to retrieve/browse mailboxes on an office 365 account? – IbrarMumtaz Nov 09 '17 at 12:01
  • 1
    I tried exact code, but got error "The Autodiscover service couldn't be located". – PerlDev Jun 22 '18 at 20:09
  • @PerlDev - Did you ever resolve your issue? I am having the exact same problem . – Dave Jun 29 '18 at 18:47
  • The following code works for me: _service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); _service.UseDefaultCredentials = true; _service.AutodiscoverUrl(appUserEmail); – PerlDev Oct 02 '18 at 20:43
  • 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 - https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth – Praveen Nayak Nov 22 '20 at 01:14
4

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.

Derorrist
  • 2,753
  • 1
  • 17
  • 26
2

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.

GeeBee
  • 33
  • 5
2

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.

Sandeep Kumar
  • 751
  • 1
  • 6
  • 7
  • Thank you, great documentation :-) – Muflix Jan 22 '21 at 17:04
  • It seems SMTP Auth will be still supported: ["Today, we are announcing that, effective October 1, 2022, we will begin to permanently disable Basic Auth in all tenants, regardless of usage, with the exception of SMTP Auth.Today, we are announcing that, effective October 1, 2022, we will begin to permanently disable Basic Auth in all tenants, regardless of usage, with the exception of SMTP Auth."](https://techcommunity.microsoft.com/t5/exchange-team-blog/basic-authentication-and-exchange-online-september-2021-update/ba-p/2772210) – DirectionUnkown Jan 05 '22 at 13:42
  • So we should register our application in our Azure ID's in which the mail box has access to send the mail? – santhoshraj Feb 02 '22 at 07:23