EWS Java API 2.0 - The Autodiscover service couldn't be located

ExchangeOnline444 21 Reputation points
2021-02-12T05:31:14.183+00:00

Hi,

I am using EWS Java API 2.0 to access user mailboxes from Exchange 2019 and O365 using autodiscoverUrl() method as EWS URL might change with new Client Access servers.

PFB the code snippet:

 private static ExchangeService exchangeService;

    private static void usingAutodiscovery() {
        exchangeService = new ExchangeService();
        try {
            exchangeService.setTraceEnabled(true);
            exchangeService.setCredentials(new WebCredentials("username", "password", "domain.com"));
            // Set the URL.
            exchangeService.autodiscoverUrl(username@domain.com, new RedirectionUrlCallback());

            Folder inbox = Folder.bind(exchangeService, WellKnownFolderName.Inbox);
            System.out.println("messages: " + inbox.getTotalCount());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        usingAutodiscovery();
    }

    public static class RedirectionUrlCallback implements IAutodiscoverRedirectionUrl {
        public boolean autodiscoverRedirectionUrlValidationCallback(String redirectionUrl) {
            return redirectionUrl.toLowerCase().startsWith("https://");
      }
    }
}

But I am getting the below error:

microsoft.exchange.webservices.data.autodiscover.exception.AutodiscoverLocalException: The Autodiscover service couldn't be located.
    at microsoft.exchange.webservices.data.autodiscover.AutodiscoverService.internalGetLegacyUserSettings(AutodiscoverService.java:742)
    at microsoft.exchange.webservices.data.autodiscover.AutodiscoverService.getLegacyUserSettings(AutodiscoverService.java:521)
    at microsoft.exchange.webservices.data.autodiscover.AutodiscoverService.internalGetLegacyUserSettings(AutodiscoverService.java:959)
    at microsoft.exchange.webservices.data.autodiscover.AutodiscoverService.getUserSettings(AutodiscoverService.java:1846)
    at microsoft.exchange.webservices.data.core.ExchangeService.getAutodiscoverUrl(ExchangeService.java:3615)
    at microsoft.exchange.webservices.data.core.ExchangeService.autodiscoverUrl(ExchangeService.java:3572)
    at com.sample.autodiscover.UsingAutodiscover2.usingAutodiscovery(UsingAutodiscover2.java:47)
    at com.sample.autodiscover.UsingAutodiscover2.main(UsingAutodiscover2.java:60)

We have below details configured:
AutodiscoverInternalUri = https://autodiscover.domainname.com/Autodiscover/Autodiscover.xml
External EWS URL = https://domainname.com/ews/exchange.asmx

Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
526 questions
Microsoft Exchange Hybrid Management
Microsoft Exchange Hybrid Management
Microsoft Exchange: Microsoft messaging and collaboration software.Hybrid Management: Organizing, handling, directing or controlling hybrid deployments.
1,981 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Xzsssss 8,861 Reputation points Microsoft Vendor
    2021-02-15T02:42:13.55+00:00

    Hi @ExchangeOnline444 ,

    Try to replace

    exchangeService.setCredentials(new WebCredentials("username", "password", "domain.com"));  
    

    with

    exchangeService.setCredentials(new WebCredentials("emailAddress", "password"));  
    

    Also test the autodiscover service with ECRCA
    Regards,
    Lou


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.