Salta al contenuto
Note per Studenti E-mail - SMTP, POP3, IMAP and MIME Report

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: and To: lines in the message are the content. The addresses servers use for delivery are those of the SMTP dialogue, MAIL FROM and RCPT TO (the envelope): they may differ. With a blind copy (Bcc) the server receives an extra RCPT TO that 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: 220 service ready, 221 service closing, 250 command completed, 354 start mail input, 421 service 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 closed

220, 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 12\tfrac12 RTT and sends 220, arriving after another 12\tfrac12 RTT: 2 RTT up to the greeting, then 6 pairs. Neglecting transmission times about 2+6=82+6=8 RTT: with RTT=50\text{RTT}=50 ms, 8⋅50=4008\cdot50=400 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:

  1. the client opens a connection to the server on TCP port 110;
  2. sends user name and password;
  3. 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 off

The 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 26=642^6=64 values and the alphabet has 26+26+10+2=6426+26+10+2=64 printable characters that pass through mail safely; 3⋅8=24=4⋅63\cdot8=24=4\cdot6 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 43\frac43 (+33%+33\%). Values: A=0,…,Z=25\texttt{A}=0,\dots,\texttt{Z}=25, a=26,…,z=51\texttt{a}=26,\dots,\texttt{z}=51, 0=52,…,9=61\texttt{0}=52,\dots,\texttt{9}=61, +=62+=62, /=63/=63. 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 4⌈n/3⌉4\lceil n/3\rceil for nn bytes.

Example. Man =01001101 01100001 01101110=01001101\ 01100001\ 01101110 (ASCII 77,97,11077,97,110) →\to 010011 010110 000101 101110 =19,22,5,46→T,W,F,u=19,22,5,46\to\texttt{T},\texttt{W},\texttt{F},\texttt{u} == TWFu. Ma →\to TWE=, M →\to TQ==, Ciao →\to Q2lhbw==. For Ma (16 bits): two groups of 6 and 4 leftover bits completed with two zeros (010011 010110 0001+00 =19,22,4→TWE=19,22,4\to\texttt{TWE}), one =. For M (8 bits): 010011 010000 =19,16→TQ=19,16\to\texttt{TQ}, two =.

Attachment size. 3⋅10242=3 145 7283\cdot1024^2=3\,145\,728 bytes become 4⋅3 145 728/3=4 194 3044\cdot3\,145\,728/3=4\,194\,304 characters; with a line break every 76 characters, ⌈4 194 304/76⌉=55 189\lceil4\,194\,304/76\rceil=55\,189 lines of 2 extra bytes each give 4 194 304+2⋅55 189=4 304 6824\,194\,304+2\cdot55\,189=4\,304\,682 bytes, a factor 4 304 682/3 145 728≈1.374\,304\,682/3\,145\,728\approx1.37 (33%33\% from base64 plus 2.6%2.6\% 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

Teoria collegata