Advertisment

How E-mail Works

author-image
PCQ Bureau
New Update

You can send e-mail in two ways: by logging onto a Web-based e-mail service like amexmail

(www.amexmail.com), or by using an e-mail client like Outlook Express or Eudora. Sending e-mail from a Web-based service is quite easy. You log on to the service, compose your mail, and press the Send button. This is so easy that you don’t even have the faintest idea of how the e-mail is actually sent.

Advertisment

On the other hand, when you use e-mail client software, you get some idea about what is happening behind the scene. You give your e-mail client software the names or IP addresses of two servers–the SMTP and POP3 servers–and the username and password required to fetch the mail. Once this is done, you ask the e-mail client to fetch or send mail for you. In this article we’ll see what really happens behind the scenes when you send e-mail. We’ll also discuss the semantics of the SMTP (Simple Mail Transfer Protocol) protocol, as specified in RFC 821. In a separate article in this issue (see ‘Coding a Mail Client in C#’, Page 101) , we will write an e-mail client using C# (C Sharp).

The following diagram gives a general idea about how e-mail actually gets sent.

Advertisment

At the core of sending e-mail is the SMTP server. The job of this server is to receive e-mail from the Web-based mail service or an

e-mail client and send it to the destination SMTP server.

The process starts when you type your message and request the Web-based mail service or the e-mail client to send it. They then request the operating system’s socket environment to resolve the name of the SMTP server to an IP address. The reason the socket environment is asked to perform this is because sockets are the service access points in the application layer in the TCP/IP protocol stack, and uniquely identify the channel from which the TCP/IP application layer receives packets from the lower layers. They are numbered from 0 to 65535, and SMTP has the socket address (often called port number) 25, assigned to it by IANA, the authority that assigns socket port numbers to various protocols.

The socket environment, in turn, queries the DNS (Domain Name Service) servers to resolve the SMTP server name to an IP address. The DNS server, upon a successful resolution, returns the IP address to the socket environment, which returns it to the e-mail client.

Advertisment

Once the IP address is received, a TCP (Transmission Control Protocol) connection is made to the SMTP server at socket port 25. The SMTP server, meanwhile listens for SMTP requests by clients on port 25 by means of a thread called MUA (Mail User Agent). When the MUA detects an incoming connection, the SMTP server (in form of the MUA) and SMTP client talk the SMTP protocol to transfer the mail from the mail client to the SMTP server. 

Once the mail is delivered to the SMTP server, the connection between the SMTP server and SMTP client is closed, and the MUA starts to listen for further incoming connections. Actually, when the MUA accepts an incoming connection, and before it starts to entertain the new connection, it spawns another MUA thread so that while it is processing mail from the incoming connection, further incoming connections don’t get blocked.

Once the mail has been transferred, another SMTP server thread, called MTA (Mail Transfer Agent), comes into play. This thread checks whether the mail is local or remote. It does so by breaking up the e-mail address, username@domain.ext, into the username and domain.ext portions. If domain.ext is registered with the SMTP server, then the mail is treated as local. The MTA then proceeds to check whether a user with the given username exists in the SMTP server’s database. In case it does, the mail is delivered to the recipient’s inbox. If no such user exists in the database, the SMTP server bounces the mail back to the sender, telling him that mail can’t be delivered.

Advertisment

Now, when you send mail over the Internet, how does the DNS know which is the SMTP server that is supposed to receive mail for a given domain name? MX records help them find an answer to this question. Whenever we set up an e-mail server, we get an MX record registered in the DNS. The MX record indicates that all mail addressed to that domain should be delivered to the server mentioned in the record.

Now, if the domain.ext mentioned in your e-mail is not registered with your SMTP server, the MTA queries the global DNS system for the IP address of domain.ext and its MX records. There can be multiple MX records for a given domain name to provide for redundancy, in case one mail server goes down. Each record has a priority number associated with it, with the lower the number, the higher being the priority. 

Once the correct SMTP server is identified via the MX record, the MTA proceeds to connect to the SMTP servers, in order of their priority. The mail is delivered to the first SMTP server that responds to the MTA, which then verifies the user name, and delivers the mail to the recipient’s inbox.

Advertisment