0

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)
  • 1) Check if you have access to the Exchange server (f/w or some restrictions could be in place). If that is fine, 2) check if auto discovery is setup on the exchange server, this article might help: https://support.practicepanther.com/en/articles/480110-fixing-the-autodiscover-service-couldn-t-be-located-error – Ironluca Feb 11 '21 at 07:56
  • @Ironluca, yes I have access to the Exchange server(checked using telnet) and yes autodiscover is setup on the Exchange server, but we are still getting the same exception. We are basically trying to access On-prem and Online mailboxes in our Hybrid environment. Please suggest if we are following the right approach. – ExUser40 Feb 23 '21 at 03:55

0 Answers0