MX Records: How Mail Finds Your Server
See how MX records route email, what preference numbers mean and how to change mail providers without losing messages.
Key takeaways
- Each MX record holds a preference number and the hostname of a mail server; lower numbers are tried first.
- The target must be a hostname with its own A or AAAA records, never an IP address or a CNAME.
- Records with equal preference share the load, while higher numbers act as fallbacks.
- When switching providers, keep the old mailboxes open until stragglers stop arriving.
An MX (mail exchanger) record tells the rest of the world which servers accept email for your domain. When someone sends a message to [email protected], their mail system looks up the MX records for example.com and delivers to one of the servers listed, trying the most preferred first.
MX records only govern incoming mail. They do not decide which servers may send on your behalf, and they have no effect on your website. Get them right and mail arrives; get them wrong and messages queue, bounce or go to a server you stopped paying for.
Reading an MX record
example.com. 3600 IN MX 10 mail1.example.com.
example.com. 3600 IN MX 20 mail2.example.com.
mail1.example.com. 3600 IN A 192.0.2.25
mail2.example.com. 3600 IN A 198.51.100.25 The first line says: mail for example.com can go to mail1.example.com, with preference 10. The second offers mail2 with preference 20. The numbers have no unit and matter only relative to each other. 10 and 20 behave exactly like 1 and 2; the gaps simply leave room to insert something later.
The name on the left is the domain part of the address. If you want mail for [email protected], that subdomain needs MX records of its own.
How delivery works
Mail servers talk to each other using SMTP, the Simple Mail Transfer Protocol. Your own mail app usually uses different ports and settings to submit and read messages, described in SMTP, IMAP and POP3. The full journey of a message is in how email works.
Preference, backup and balance
The sending server sorts the MX records by preference and starts with the lowest number.
A permanent rejection is different from a failure to connect. If your server answers and says the mailbox does not exist, the sender normally bounces the message and does not shop around. Temporary errors and unreachable servers lead to retries. Details of the notices are in what happens when an email bounces.
Do you need a backup MX?
A lower-preference fallback helps only if it handles mail properly. A backup that accepts anything and forwards later needs the same spam filtering and the same list of valid addresses as the main server. Otherwise spammers target it, and you end up generating bounces to forged senders. Since legitimate senders already queue and retry, many small domains are better off with the records their provider specifies and no improvised extra.
Rules that are easy to break
- Use a hostname.
MX 10 192.0.2.25is invalid. - No alias as the target. The MX target should have A or AAAA records directly, not a CNAME.
- No CNAME at the domain itself. A name with a CNAME cannot also have MX records. See the apex problem.
- Mind the trailing dot in zone files, or your domain gets appended to the target.
- Remove old records when you change providers.
If a domain has no MX record at all, the standard says senders may fall back to its A or AAAA address. So mail might be attempted against your web server. If a domain should never receive email, publish a null MX:
example.com. 3600 IN MX 0 . That single record, with preference 0 and a lone dot, tells senders to give up immediately.
MX is only part of email DNS
Receiving works with MX alone. To have your outgoing mail trusted, you also need records that describe who may send:
| Record | Purpose |
|---|---|
| SPF (a TXT record) | Lists servers allowed to send for the domain |
| DKIM (TXT or CNAME) | Publishes keys that verify message signatures |
| DMARC (TXT) | Says what receivers should do when checks fail |
| PTR | Maps your sending server's address back to a name |
See SPF, DKIM, DMARC and reverse DNS. The TXT format itself is covered in TXT records.
Using a hosted mail provider
Providers give you the exact records to publish. Some give one hostname, others several with different preferences. The steps are broadly the same:
- Verify you own the domain, usually with a TXT record.
- Create all mailboxes and aliases at the new provider before changing MX.
- Add the provider's MX records exactly as given.
- Delete the previous MX records.
- Update SPF, DKIM and DMARC.
- Send test messages in both directions from an outside account.
For a first setup, setting up email on your own domain covers the choices around this.
Switching without losing mail
MX records are cached like any other, so for a while some senders will use the old servers.
- Lower the MX TTL a day or more ahead.
- Keep the old mailboxes active for several days after the change, and collect anything that lands there.
- Do not run both providers' records together in the hope of covering both. Mail would scatter between two sets of mailboxes.
- Copy old mail across, either before the switch with a final catch-up afterward, or with the provider's import tool.
A fuller plan is in migrating company email.
Changing DNS hosts for your website can also break mail: if the new zone is missing the MX records, delivery fails even though the website looks fine.
Filtering services in front of your mailbox
Some organizations send incoming mail through a filtering gateway before it reaches the mailbox provider. In that design the MX records point at the filter, and the filter is configured to pass clean mail onward to the real mail server.
Two details matter. First, the mailbox system should accept incoming mail only from the filter. Otherwise a spammer who knows or guesses the provider's hostname can deliver straight to it and skip the checks. Second, forwarding through a gateway changes the apparent source of each message, so the receiving system has to be told to trust the gateway, or sender checks such as SPF may appear to fail.
Encryption in transit
An MX lookup says where to deliver, but nothing in the record requires encryption. Mail servers usually upgrade the connection with a command called STARTTLS when both support it. Because that upgrade is opportunistic, an attacker able to tamper with the connection or the DNS answer could try to prevent it or redirect the mail.
Additional standards let a receiving domain say that senders should insist on a valid encrypted connection. One, MTA-STS, publishes a policy over HTTPS announced by a TXT record. Another, DANE, relies on DNSSEC to publish certificate information for the mail host. Hosted providers differ in which they support. Neither makes email end-to-end encrypted: the message is readable on the servers that handle it, as discussed in is email encrypted.
Subdomains and separate mail streams
Each name has its own MX records. example.com having them does nothing for news.example.com. That is useful: you can point a subdomain's mail at a help desk or ticketing system while the main domain stays with your mailbox provider.
example.com. IN MX 10 mail.example.net.
help.example.com. IN MX 10 inbound.example.org.
noreply.example.com. IN MX 0 . The last line declares that a send-only name accepts nothing. Think before doing that, because recipients who reply will get an immediate failure. For addresses people might answer, it is kinder to receive the replies somewhere.
Checking your records
On macOS or Linux:
dig example.com MX +short
dig mail1.example.com A +short On Windows:
Resolve-DnsName example.com -Type MX
nslookup -type=mx example.com To confirm a server is listening, test the port from a network that permits it:
Test-NetConnection mail1.example.com -Port 25 Many home and some cloud networks block outbound port 25 to limit spam, so a failure there may say more about your connection than about the mail server. Test only servers you are responsible for or are sending mail to in the ordinary way.
Symptoms and likely causes
- No incoming mail at all: missing MX, wrong target, expired domain or a firewall blocking port 25.
- Some mail arrives at the old system: cached records, or an old MX still published.
- Senders report "domain not found": delegation or name server trouble with the whole domain.
- Sending works but receiving does not: sending uses your provider's servers directly, so it can succeed while your MX is wrong.
- Mail from a hosting server to your own domain never arrives: that server may still think it handles the domain locally and skip the MX lookup. Disable local delivery for the domain there.
A quick pre-flight list
Mail faults are often noticed late, because nobody sees the message that never came. Before you call a mail setup finished, confirm that the MX targets resolve to addresses, that no stale record remains, that the mailboxes for common addresses such as postmaster exist, and that an outside sender receives a reply. Recheck after any DNS migration, since MX records are among the easiest to leave behind.