Mail basics

Mail Transport Agent (MTA)

A program which transfers an E-mail message from one mailbox to another, usually over a network.

Mail User Agent (MUA)

A program which allows the user to enter a new E-mail message, and read received E-mail

Examples: ...

Most MUAs have only a very simple MTA built in. They can send outgoing mail using SMTP to one remote machine only (a smarthost) which in turn has to take responsibility for relaying it to the remote destination, or returning it if delivery failed. Hence one of the services you need to provide as an ISP is a "Smarthost" or "Outgoing SMTP Relay".

       MUA ---------> MTA --------- - - - - ---------->MTA
                   (smarthost)                     destination

The destination machine must be connected to the Internet 24x7, so that it is always able to receive mail. However MUAs are usually designed for disconnected operation; they expect the final MTA is always connected to the Internet, and collect mail from this using POP3 or IMAP. Hence the other service you need to provide as an ISP is a POP3/IMAP server.

What happens if we want to send a message to ?

We have to decide where to send it next.

We can do the same lookups by hand. Example:

Recipient is

$ nslookup -q=mx        preference = 10, mail exchanger =

(i.e. send mail for to machine ''. Note that this will require a second DNS lookup to get the A record for, so that we can open an SMTP connection to it.)

Recipient is

$ nslookup -q=mx       preference = 10, mail exchanger =       preference = 10, mail exchanger =       preference = 10, mail exchanger =       preference = 10, mail exchanger =       preference = 30, mail exchanger =

Recipient is

# nslookup -q=mx
        origin =
        mail addr =
        serial = 2001050800
        refresh = 3600 (1H)
        retry   = 1800 (30M)
        expire  = 604800 (1W)
        minimum ttl = 3600 (1H)

(i.e. No MX records found)

# nslookup -q=a


Now we know where to sending it, try sending it to each of the hosts in turn. If we are using MX records, start with the lowest preference value first.


SMTP is a text-based protocol, and you can talk SMTP directly using the keyboard. This is an extremely useful debugging tool. A machine which accepts SMTP mail listens on TCP port 25.

There are only five SMTP commands you need to know:

HELO            -- announce my own hostname (this is usually ignored)
MAIL FROM:<..>  -- return address ("envelope sender")
RCPT TO:<..>    -- recipient address, can be repeated multiple times
DATA            -- start of the actual message. Send the headers, 
                   a blank line, the message, and end with a period on
                   a line of its own
QUIT            -- end the SMTP session

The responses are three-digit numbers (so that they are easily understood by other mail systems), with some optional text displayed for humans to read.

2xx      -- OK
3xx      -- Go ahead
4xx      -- Temporary failure, retry later
5xx      -- Permanent failure

Exercise: delivering mail via SMTP by hand

Now use SMTP to deliver a message to your own machine:

$ telnet 25
Connected to
Escape character is '^]'.
220 ESMTP Sendmail 8.11.1/8.11.1; Wed, 9 May 2001 10:43:38 GMT
HELO dontcare
250 Hello [], pleased to meet you
MAIL FROM:<yourhome@email.address>
250 2.1.0. <yourhome@email.address>... Sender ok
250 2.1.5 <>... Recipient ok
354 Enter mail, end with "." on a line by itself
Subject: Test message

This is a test message
250 2.0.0 l43AtfS00230 Message accepted for delivery
221 2.0.0 closing connection
Connection closed by foreign host.

NOTE: If you need to drop out of a telnet session, hit ctrl and ] then type close

Message envelope

The MAIL FROM and RCPT TO commands together form the message "envelope": that is, the delivery instructions for the message (where to send it to, and where to return it if delivery fails). Notice that these could be completely different from the message headers, including To: and From:

This is often the case for messages which have been distributed via mailing lists. The message headers may say:


But once the mailing list has mailed out separate copies of this message to all the subscribers, these copies will have their own envelope:


The subscriber's address will be in RCPT TO, so they receive it, and usually the envelope sender is changed to the list administrator. If there is a problem with this subscriber's address, the person who needs to know is the list administrator, not the person who originally posted the message.


SMTP is defined in RFC 2821. The format of an E-mail message is defined in RFC 2822. (The older versions of these documents were RFC 821 and RFC 822, and you still may find people referring to "RFC822 headers")