E-mail - SMTP, POP3, IMAP and MIME
In questa pagina 7
Problem and architecture
E-mail is different from HTTP (HTTP and the WebThe application layer is the top of the stack and only uses transport services; the Web is a distributed client-server hypertext service whose pages have URLs of the form protocol://host:port/path; HTTP runs over TCP (server port 80), is stateless and uses text messages (request or status line, headers, blank line, body) with methods GET, POST, HEAD, PUT, DELETE and status codes 2xx to 5xx; a page with N embedded objects needs 2(N+1) RTT with non-persistent connections and N+2 RTT with a persistent connection (transmission times neglected), cookies give HTTP a memory through Set-Cookie and Cookie headers, and a proxy (web cache) keeps copies of recent responses to cut load, traffic and delay.HTTP and the Web →): there the server is always up and waits for a client. In mail:
- e-mail is a one-way transaction: if B replies, that is another one-way transaction;
- it is neither possible nor logical for B to run a server program waiting for someone to write.
The client/server idea is therefore realised with intermediate computers (servers).
Definition (typical scenario). Two UAs (User Agents) on local hosts; two push client/server programs, the MTAs (Message Transfer Agents); one pull client/server program, the MAA (Message Access Agent).
UA of Mario UA of Luisa
| SMTP sender's server ---------SMTP--------> recipient's |
|--------> (e.g. Gmail) (MTA - MTA) server <------------|
| (MTA) (e.g. Yahoo) POP3 or IMAP4
(MAA)Three uses of the paradigm: (1) sender to its server: MTA, SMTP; (2) sender's server to recipient's server: MTA, SMTP; (3) recipient's server to recipient: MAA, POP3 or IMAP4 (or HTTP for webmail). SMTP is push: whoever has a message sends it to a server that accepts it; the recipient must pull when connected. Transport is TCP (TCP - Connection, Reliability and Flow ControlTCP is the connection-oriented, reliable transport protocol that turns IP's unreliable service into an ordered byte stream without errors or duplicates; the connection opens with a three-way handshake (SYN, SYN+ACK, ACK) and closes with FIN segments, bytes are numbered (sequence number = first byte of the segment, cumulative ACK = next byte expected), the sender may transmit min(rwnd, cwnd) unacknowledged bytes (rwnd, the receiver window in a 16-bit field, is flow control), errors are handled with checksum, ACKs, retransmission timeout and fast retransmit after three duplicate ACKs, and to fill the pipe the window must reach the bandwidth-delay product, with maximum throughput MSS times Wmax over RTT.TCP - Connection, Reliability and Flow Control →); the destination server is found with DNS (MX record: DNS - Domain Name SystemDNS translates names such as www.amazon.com into IP addresses; it is a distributed hierarchical database (an inverted tree of up to 128 levels, with 13 root servers and domains registered through ICANN-accredited registrars), each ISP has a local DNS whose address the host gets from DHCP, the host asks it (usually over UDP) and it queries root, top-level and organisation servers, every server caches what it learns marking answers as non-authoritative until the TTL expires, records are (name, type, value, class, TTL), with NAT the DNS must return the public address, and there are four types of attack including cache poisoning.DNS - Domain Name System →).
User Agent: writes, reads, replies, forwards messages and manages local mailboxes. Command-driven (mail, pine, elm) or graphical (Eudora, Outlook).
The message
ASCII text: a header (lines Name: value: From, To, Date, Subject), a blank line, and the body.
From: Behrouz Forouzan
To: William Shane
Date: 1/10/2011
Subject: Network
Dear Mr. Shane,
We want to inform you that ...Definition (envelope and content). The
From:andTo:lines in the message are the content. The addresses servers use for delivery are those of the SMTP dialogue,MAIL FROMandRCPT TO(the envelope): they may differ. With a blind copy (Bcc) the server receives an extraRCPT TOthat is absent from the content.
SMTP
SMTP (Simple Mail Transfer Protocol) defines the commands and replies exchanged between an MTA client and an MTA server; each ends with CR LF.
- Commands (client to server):
HELO(client introduces itself with its domain name),MAIL FROM,RCPT TO,DATA,QUIT. - Replies: a three-digit code, possibly followed by text:
220service ready,221service closing,250command completed,354start mail input,421service not available.
Property (three phases). Opening, mail transfer, closing.
1. Opening. After the client opens a TCP connection to port 25: the server sends 220 (or 421 if not ready); the client sends HELO; the server replies 250.
2. Transfer. (1) client MAIL FROM (the address to which errors are returned); (2) server 250; (3) client RCPT TO; (4) server 250; (5) client DATA; (6) server 354; (7) client sends the message line by line, each ending with CR LF, and ends with a line containing only a dot; (8) server 250.
3. Closing. Client QUIT, server 221.
S: 220 service ready
C: HELO some.com
S: 250 OK
C: MAIL FROM: forouzan@some.com
S: 250 OK
C: RCPT TO: william@aNetwork.com
S: 250 OK
C: DATA
S: 354 start mail input
C: From: Behrouz Forouzan
C: To: William Shane
C: Date: 1/10/2011
C: Subject: Network
C:
C: Dear Mr. Shane,
C: We want to inform you that ...
C: .
S: 250 OK
C: QUIT
S: 221 service closed220, HELO, 250 are the opening; MAIL FROM and RCPT TO with replies form the envelope; after DATA and 354 come header, blank line and body, closed by the dot; QUIT and 221 are the closing. There are 6 command-reply pairs (HELO, MAIL FROM, RCPT TO, DATA, the text ending with ., QUIT) plus the initial 220 sent by the server on its own.
Session duration. Each command-reply pair costs one RTT (SMTP waits for the reply before the next command). The client gets the SYN+ACK after 1 RTT and sends the ACK; the server receives it after RTT and sends 220, arriving after another RTT: 2 RTT up to the greeting, then 6 pairs. Neglecting transmission times about RTT: with ms, ms for one message. This is why servers keep the connection open and send several messages in a row.
POP3
POP3 (Post Office Protocol, version 3) is simple but limited. Access starts from the client when it must download mail from its mailbox on the server:
- the client opens a connection to the server on TCP port 110;
- sends user name and password;
- can list (
LIST) and retrieve (RETR) messages one by one.
Commands: USER, PASS, STAT, LIST, RETR n, DELE n, QUIT; replies +OK or -ERR.
S: +OK POP3 server ready
C: USER luisa
S: +OK
C: PASS ********
S: +OK maildrop has 2 messages (320 octets)
C: LIST
S: +OK 2 messages
S: 1 120
S: 2 200
S: .
C: RETR 1
S: +OK 120 octets
S: (message 1 here)
S: .
C: QUIT
S: +OK POP3 server signing offThe traditional mode is download and delete: messages move to the user's computer and disappear from the server.
IMAP4
POP3 limits: it does not let the user organise mail on the server; no different folders on the server; no partial inspection of a message before downloading it. IMAP4 (Internet Mail Access Protocol, version 4) adds: checking a message header before download; searching a string in message contents before downloading; downloading a message partially; creating, deleting, renaming mailboxes on the server; a hierarchy of mailboxes (folders).
| POP3 | IMAP4 | |
|---|---|---|
| where messages live | on the user's computer after download | on the server |
| folders on the server | no | yes, hierarchical |
| check before download | no | header, search, partial download |
| TCP port | 110 | 143 |
With a mailbox read from a PC and a phone, IMAP4 shows the same messages and folders on both; with POP3 and download-and-delete each device would have only what it downloaded.
MIME
Plain mail can only carry 7-bit ASCII (NVT): only Latin-alphabet languages, no binary files such as images and video. MIME (Multipurpose Internet Mail Extensions) is a supplementary protocol acting as a translator to ASCII, adding headers such as MIME-Version: 1.0, Content-Type (e.g. image/png) and Content-Transfer-Encoding.
Definition (base64). Takes data 3 bytes at a time (24 bits), splits them into 4 groups of 6 bits and writes each group (value 0 to 63) with a character of the alphabet
A-Z a-z 0-9 + /.
Why 6 bits and 3 bytes: 6 bits give values and the alphabet has printable characters that pass through mail safely; is the smallest number of bits that is a multiple of both 8 and 6. So 3 bytes become exactly 4 characters and the body grows by (). Values: , , , , . If the byte count is not a multiple of 3, one or two = characters pad the end. Lines are at most 76 characters. Output length is for bytes.
Example. Man (ASCII ) 010011 010110 000101 101110 TWFu. Ma TWE=, M TQ==, Ciao Q2lhbw==. For Ma (16 bits): two groups of 6 and 4 leftover bits completed with two zeros (010011 010110 0001+00 ), one =. For M (8 bits): 010011 010000 , two =.
Attachment size. bytes become characters; with a line break every 76 characters, lines of 2 extra bytes each give bytes, a factor ( from base64 plus for line breaks).
Quoted-printable suits mostly-ASCII text: non-ASCII characters become = plus the two hexadecimal digits of the byte (in UTF-8 è is bytes C3 A8, written =C3=A8).
Common mistakes
Saying SMTP is used to read mail (it sends and relays; reading is POP3, IMAP4 or HTTP); confusing envelope (MAIL FROM, RCPT TO) and From:/To: headers; forgetting the line with only . ending the message (and the blank line before the body); thinking base64 is encryption (it is only an encoding, reversible by anyone); counting 3 bytes as 3 base64 characters (it is 4) or forgetting the = padding; swapping port 110 (POP3) with 25 (SMTP).
Versione ripasso
- Mail is a one-way transaction, so intermediate servers: 2 UAs, MTAs (push, SMTP: sender to its server, server to server), MAA (pull, POP3 or IMAP4, or HTTP for webmail). Transport TCP; destination found with the DNS MX record (DNS - Domain Name SystemDNS translates names such as www.amazon.com into IP addresses; it is a distributed hierarchical database (an inverted tree of up to 128 levels, with 13 root servers and domains registered through ICANN-accredited registrars), each ISP has a local DNS whose address the host gets from DHCP, the host asks it (usually over UDP) and it queries root, top-level and organisation servers, every server caches what it learns marking answers as non-authoritative until the TTL expires, records are (name, type, value, class, TTL), with NAT the DNS must return the public address, and there are four types of attack including cache poisoning.DNS - Domain Name System →).
- Message: header (
From,To,Date,Subject), blank line, body. Envelope (MAIL FROM,RCPT TO) differs from content headers (Bcc). - SMTP, TCP port 25, three phases: opening (
220,HELO,250), transfer (MAIL FROM/250,RCPT TO/250,DATA/354, text ended by a line with only./250), closing (QUIT/221).421= unavailable. 6 command-reply pairs plus the220. - Duration: 2 RTT to the greeting plus 6 pairs RTT ( ms at RTT ms); see TCP - Connection, Reliability and Flow ControlTCP is the connection-oriented, reliable transport protocol that turns IP's unreliable service into an ordered byte stream without errors or duplicates; the connection opens with a three-way handshake (SYN, SYN+ACK, ACK) and closes with FIN segments, bytes are numbered (sequence number = first byte of the segment, cumulative ACK = next byte expected), the sender may transmit min(rwnd, cwnd) unacknowledged bytes (rwnd, the receiver window in a 16-bit field, is flow control), errors are handled with checksum, ACKs, retransmission timeout and fast retransmit after three duplicate ACKs, and to fill the pipe the window must reach the bandwidth-delay product, with maximum throughput MSS times Wmax over RTT.TCP - Connection, Reliability and Flow Control → for the handshake.
- POP3, port 110:
USER,PASS,STAT,LIST,RETR,DELE,QUIT;+OK/-ERR; download and delete, messages one by one, no server folders. - IMAP4, port 143: header preview, search, partial download, create/delete/rename mailboxes, folder hierarchy; messages stay on the server (same view on PC and phone).
- MIME: non-ASCII data into 7-bit ASCII; headers
MIME-Version,Content-Type,Content-Transfer-Encoding. - base64: 3 bytes (24 bits) 4 groups of 6 bits 4 characters of
A-Z a-z 0-9 + /;=padding; size ();ManTWFu,MaTWE=,MTQ==,CiaoQ2lhbw==; 3 MiB attachment bytes ( with 76-character lines). Quoted-printable:=C3=A8. - Typical errors: SMTP to read mail; base64 as encryption; missing
.line; ports 110 vs 25.