How to speed up slow EWS autodiscover with Office 365
Autodiscover is running really slow
I recently made basic digital signage solution for work that enabled me to show all the meetings of the day on a monitor. After a lot of searching and experimenting, everything worked well on my computer, but it did not on my cheap OVH VPS. As I’m using the Java Managed API to connect to Exchange Web Services, with the OpenJDK JRE it didn’t work at all, it kept loading for over ten minutes, just to get a connection error in the end for my SSH connection. And with the Oracle JRE it did sometimes, but with a lot of effort. Sometimes it worked after a minute, other times it kept on loading as long as with OpenJDK installed.
After some debugging and searching the internet for possible causes, I found a lot of technical explanations that didn’t help me in my situation. It was more about how to fix the slow autodiscover search in the Outlook mail client. But this guide is not how to fix slow autodiscover in Microsoft Outlook! This guide is about speeding it up with Office 365, by using the Managed API, whether it’s the C# one or the Java one.
I found out that I could test and investigate the routing of my domain’s autodiscover with Microsoft’s remote connectivity analyzer, which is available at https://testconnectivity.microsoft.com/. To check out your routing via autodiscover and Office 365, click on the Office 365 tab and there on Outlook autodiscover. Press next, fill in all the details and perform the test to find out what route Outlook and EWS take to find out where to search for the mail account.
Connecting directly to Exchange Online (Office 365)
If you’re just connecting to Office 365 for your mail (and maybe have a hybrid setup for AD syncing), there’s no need to perform a full autodiscover route. We just need the direct link to Exchange Online (and not the autodiscover links). By the way, this would also help if you only have one Exchange server and know the link to that server. Let me tell you how that I found it out, maybe it can help you in other situations.
You can find out with the EWS Managed API by using the method
getUrl() on your already established connection. You should be able to at least make a connection to Office 365 EWS already, but let me just sum it up in short for you in Java:
// For Office 356 EWS, you need to use ExchangeVersion.Exchange2010_SP2 service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); ExchangeCredentials credentials = new WebCredentials("firstname.lastname@example.org", "password"); service.setCredentials(credentials);
When you’ve set that up you need to set up the URL to connect to, instead of starting an autodiscover request. The URL to Exchange Online is
https://outlook.office365.com/EWS/Exchange.asmx. You can integrate it in your code like this:
// Instead of performing an autodiscover request... //service.autodiscoverUrl("email@example.com", new RedirectionUrlCallback()); // ...you can connect directly to Exchange Online service.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx"));
That should speed up your requests drastically! In my case, it went from 10+ minutes and a fail in the end, to a total script execution of a few seconds!
Disabling SCP lookup with setEnableScpLookup(false);
If you’re on a foreign server without SCP, you can skip that feature to speed up autodiscover requests. I doubt that’s really necessary in our case because we’ll be connecting directly to the url of Exchange Online. If you do need to use autodiscover after all for some reason, try out improving the performance by disabling it. In our Exchange Online on 365 case, it doesn’t hurt to disable it.
See more at Improving performance when using Autodiscover for Exchange about that feature.