Autodiscover and DNS
An Exchange client is set up from an email address and a password. Everything else, from the protocol to use to the URL of each endpoint, comes from Autodiscover, a lookup that the client performs before it connects. Discovery that fails leaves Outlook with no way to configure the account at all, since current versions of classic Outlook have no form for entering Exchange server settings by hand. The DNS records, the certificate and the advertised URLs described on this page are therefore the part of an Exchange deployment that most needs checking.
With Exchange support enabled, Stalwart answers every form of Autodiscover that Exchange clients use. With it disabled, Autodiscover returns only IMAP, POP3 and SMTP settings, as described in Autoconfiguration.
Discovery sequence
Section titled “Discovery sequence”For an address such as [email protected], Outlook tries the following locations in turn and uses the first that answers:
https://example.com/autodiscover/autodiscover.xml, on the domain itselfhttps://autodiscover.example.com/autodiscover/autodiscover.xml- A redirect that
http://autodiscover.example.com/autodiscover/autodiscover.xmlmay return - The SRV record
_autodiscover._tcp.example.com, which names the host to ask instead
Other clients follow variations of the same order, and every client tries the autodiscover host name. Outlook also contacts Microsoft 365 first for many addresses, which Outlook for Windows explains how to prevent.
Stalwart answers on whichever host name the request arrives on: the advertised URLs come from the public URL, not from the request. A location is therefore usable as soon as its name resolves to the server and the certificate covers it. The address in the request must belong to an account that the signed-in user can access.
DNS records
Section titled “DNS records”The autodiscover host name is the location that works for every client, and Stalwart generates its record together with the domain’s other records, as described in DNS records:
autodiscover.example.com. CNAME mail.example.com.Stalwart does not generate an SRV record for Autodiscover. One is useful when the certificate cannot cover autodiscover.<domain>, for example when many domains share one server and certificate, because the client then connects to the host named in the record, whose certificate it already trusts. The record names the HTTPS port, 443:
_autodiscover._tcp.example.com. SRV 0 0 443 mail.example.com.The domain itself, the first location that Outlook tries, needs no record. When the domain’s address points to a web server elsewhere, that server answers or fails, and Outlook moves on to autodiscover.<domain>. When it points to Stalwart, Stalwart answers there as well, provided the certificate covers the domain name.
Certificate
Section titled “Certificate”Every location is fetched over HTTPS, and Exchange clients do not accept a certificate they do not trust. The certificate must cover autodiscover.<domain> and the host name in the advertised URLs, and a self-signed or private certificate must be installed as trusted on every client. The default ACME certificate of a domain includes autodiscover.<domain> and the server’s host names within the domain. A domain whose mail host lies outside it, such as example.org served from mail.example.com, is reached through the SRV record or through the autodiscover name in the domain’s own certificate.
Advertised endpoints
Section titled “Advertised endpoints”The answer to Autodiscover contains absolute URLs, which Stalwart builds from its public URL. That is STALWART_PUBLIC_URL when it is set, and otherwise a URL derived from the server’s host name as described there. For a public URL of https://mail.example.com, the endpoints are the following:
| Endpoint | URL |
|---|---|
| MAPI over HTTP, mailbox | https://mail.example.com/mapi/emsmdb/?MailboxId=<mailbox id> |
| MAPI over HTTP, address book | https://mail.example.com/mapi/nspi/?MailboxId=<mailbox id> |
| EWS | https://mail.example.com/EWS/Exchange.asmx |
| ActiveSync | https://mail.example.com/Microsoft-Server-ActiveSync |
| Offline address book | https://mail.example.com/OAB/<id>/ |
A wrong public URL is the most common reason why discovery succeeds but the client then fails to connect. Behind a reverse proxy, the public URL must be the address that the proxy serves.
Autodiscover comes in several forms, and each kind of client receives the endpoints it can use:
- Outlook posts an XML request (the POX form) to
/autodiscover/autodiscover.xml. Outlook announces that it speaks MAPI over HTTP, and receives the MAPI over HTTP endpoints, the EWS and offline address book URLs, and the IMAP, POP3 and SMTP settings of the server. The answer also lists the shared mailboxes that Outlook opens automatically, as described in Shared mailboxes. - Other EWS clients that use the POX form receive the EWS URL in place of MAPI over HTTP. Clients that use the SOAP form, at
/autodiscover/autodiscover.svc, receive the same settings throughGetUserSettings. - ActiveSync devices post a request in the mobile sync schema to the same
/autodiscover/autodiscover.xml, and receive the ActiveSync URL. - Clients that use the JSON form, such as Outlook for Mac, first query
/autodiscover/autodiscover.json. ForProtocol=AutodiscoverV1it points them to the XML form, and forProtocol=ActiveSyncandProtocol=Ewsdirectly to those endpoints.
Each of these answers depends on the account, so the XML and SOAP forms require authentication with the account’s credentials. An anonymous request receives 401, as on an Exchange server, and the client then asks the user for the password. Errors inside an XML answer use the Autodiscover error codes: 500 when the requested mailbox does not exist or is not accessible to the signed-in account, 600 for an invalid request, and 601 for a request schema that the server does not provide.
Offline address book
Section titled “Offline address book”The offline address book is a copy of the global address list that Outlook in cached mode downloads and searches locally, so that names resolve without a request to the server. Autodiscover gives Outlook its URL, and Outlook downloads it in the background.
Stalwart builds one offline address book per tenant, or one for the whole server when tenants are not used, and refreshes it when it is used. When Outlook runs Autodiscover or downloads the address book’s manifest, and the stored copy is older than one hour, the server builds a new one in the background and keeps serving the old one until the new one is ready. When the address book has changed, Outlook downloads it in full, since differential updates are not offered. A directory that has not changed produces no new version, so clients download nothing. The first Autodiscover request after Exchange is enabled starts the first build, and until it completes the answer contains no address book URL. Outlook then picks it up at its next Autodiscover check.
An account that may not list the directory receives no offline address book: no URL in Autodiscover, and 404 from the address book path. This is the case for every account while allowDirectoryQueries is disabled, except accounts with the sysAccountQuery permission.
Test discovery
Section titled “Test discovery”Discovery can be checked without a client. A request without credentials should receive 401, and the same request with credentials should receive an Autodiscover answer. The following request posts Outlook’s request schema for [email protected]:
-H 'Content-Type: text/xml' \ -H 'X-MapiHttpCapability: 1' \ --data '<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006"><Request><EMailAddress>[email protected]</EMailAddress><AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema></Request></Autodiscover>'The answer should contain a Protocol element of type mapiHttp with URLs on the expected host name. A GET of the same URL with credentials receives error 600, which confirms only that the credentials were accepted.
Discovery failures
Section titled “Discovery failures”The following symptoms each have a known cause:
- The client keeps asking for the password. The password is wrong. The same prompt appears when the
autodiscovername does not resolve to Stalwart, or a reverse proxy answers it.dig +short CNAME autodiscover.example.comshould return the server’s host name. - Sign-in fails for an account with two-factor authentication. The user entered the account password instead of an app password. The server answers such a sign-in with
402andMFA code required. - The client rejects the connection without asking for a password. The certificate does not cover the name the client asked, or it is not trusted on the client. Check with
openssl s_client -connect autodiscover.example.com:443 -servername autodiscover.example.com. - The answer contains IMAP and SMTP settings only. Exchange support is disabled, so Autodiscover gives the answer for IMAP clients.
- Discovery succeeds but the client cannot connect. The advertised URLs carry the wrong host name. Set the public URL to the address that clients reach.
- Outlook shows no offline address book. Directory queries are disabled, or the first build had not finished at the time of the last Autodiscover check.