The Domain Name System (DNS) plays an important role in e-mailing. The DNS is used by both, e-mail clients and e-mail servers. Even if you do not intend to maintain your own DNS server, a thorough understanding of DNS's role in e-mailing is a necessity for the mail server operator. This section assumes that the reader has basic knowledge of how DNS works in general.
In many networking scenarios, only two DNS record types are used—the A record and PTR record. These map hostnames to IP addresses and IP addresses to hostnames respectively. These record types are also used for e-mail, but there is also a third DNS record type that is uniquely available for e-mail.
How does an SMTP server discover to which host a message for a certain domain should be delivered? The recipient domain is, not surprisingly, used as the key in one or more DNS lookups. The first lookup that is made is for the mail-specific MX record—the mail exchanger record type. The MX entry allows the DNS operator to specify the hostname or hostnames of servers that can receive mail for a certain domain. For example, MX
records can be used to specify that messages to someone at example.com
should be sent to mail.example.com
. If the recipient domain does not have an MX
record, an attempt is made to find an A
record for the recipient domain. If the A
record lookup succeeds, the mail will be delivered to the host. If both the MX
and A
lookups do not return any results, the message is deemed undeliverable and is returned to the sender.
There are two good reasons to having MX
records:
Firstly, it might not be desirable to be forced to map the
A
record of a domain to the mail server. For example, Company Inc. with the WWW address http://www.example.com/ wants to allow visitors to use the shorterhttp://example.com/ URL, but does not want to run the web server application on the mail server (or vice versa).The more important reason is that the result of an
MX
lookup not only contains a list of hostnames, but rather a list of (hostname, priority) tuples. The priority field is an integer describing the priority of the hostname within the list. The absolute magnitude of the priority number does not matter, but it is used in relation to the priority of any other hostnames to create an ordered list of hostnames to try when delivering a message. The list is in ascending order, so the hostname with the lowest priority number will be contacted first. If two hostnames have equal priority, they will be tried in random order.
Equal-priority MX
records can be used as a very crude form of load balancing between two or more servers. This is also possible with A
records that map to multiple IP addresses. A hierarchy of backup mail servers with different priorities can be set up for a domain using MX
records that cannot be made to happen with A
records. Let us look at a constructed example of an organization that uses a lot of mail servers.
Priority |
Hostname |
---|---|
|
|
|
|
|
|
|
|
If this DNS configuration is set for the domain example.com
, SMTP servers are expected to try to deliver messages for example.com
to mx1.example.com
or mx2.example.com
first. If both connections fail, mx3.example.com
should be tried, and if even that server does not respond in a timely way, mx4.example.com
is the last resort. Should that fail too, the message is kept and delivery is retried at a later time.