Networker application layer: Email, SMTP protocol and using Telnet to send email

References:
https://www.bilibili.com/video/BV1c4411d7jb?p=72 & vd_source=e66dd25b0246f28e772d75f11c80f03c
The computer network course taught by Mr. Gao Jun of Lake University of Science and Technology, last year’s elective network planning was all thanks to Mr. Gao, vivid, rich in examples, supporting experimental courses, no nonsense and pure dry goods, just push a wave!

Introduction to Email

E-mail is a communication method that provides information exchange by electronic means, and is the most widely used service on the Internet. Through the e-mail system of the network, users can send emails to any designated destination in the world in a very low price (no matter where they are sent), and in a very fast way (send to any designated destination in the world), Connect with Internet users anywhere in the world.
E-mails can be in various forms such as text, images, and sounds. At the same time, users can get a lot of free news, special mail, and easily realize easy information search. The existence of e-mail has greatly facilitated the communication and exchange between people and promoted the development of society.

Email system

The e-mail system adopts the client/server approach.

There are three main building blocks of an email system: the user agent, the mail server, and the protocols required for email.

User Agent (LDA): The interface between the user and the email system, also known as email client software.
Many current mail clients can set the option of mail proxy, such as hotmail, Netease mailbox master, etc.

Mail Server: The infrastructure of the email system. All ISPs on the Internet have mail servers, whose function is to send and receive mail, and is also responsible for maintaining user mailboxes.

As shown in the figure, it is the process of sending email:

Email sending process

  1. The sender uses SMTP to send mail through the user agent, and this process follows the TCP protocol;
  2. The mail first arrives at the mail server of the sender, and the SMTP client is logged into the server, and the mail is sent using SMTP. This process also uses the TCP protocol;
  3. The recipient receives the mail through the SMTP server, and then transfers it to the POP3 server, and uses POP3 to read the mail. The recipient (also a POP3 client) ultimately receives the mail through the user agent.

How SMTP works

Both the sender and the receiver use the TCP protocol to connect, which has just been clarified. Note that the port number is 25.

The sending mail server is like a mailbox. The user puts the mail to be sent into the “mail box”, and the server periodically scans the mail cache. If it finds a mail to be forwarded, the server will “pick up” the mail and send it to the receiver’s server through TCP .

The entire workflow is relatively simple to outline. Let’s look at the specific TCP handshake process:

The sender and receiver handshake using a command and reply pattern. Among them, there are 14 commands and 21 types of response information.

  1. First, the receiver will actively push a response 220 to inform other servers that their services are ready.

  2. The client explains its identity to the server and informs itself of the domain name of the SMTP server.

HELO url
// The URL here is the domain name of the sender's SMTP mail server.
  1. After that, if the identity is valid, reply 250. Otherwise other codes are sent back, such as 421, indicating that the service is unavailable.
  2. The client tells the server where the mail is coming from:
MAIL FROM:url
//The URL here is the user's own email domain name.
  1. If the receiving server thinks it is reasonable, it will send back 250, otherwise it will send back other error codes.
  2. The client tells the server where the mail needs to go:
RCPT TO:url
//The url here is the address of the recipient.
  1. If the mailbox exists, send back a response code of 250, otherwise send back other error codes.
  2. The client tells the server that it is ready to send the email content: DATA
  3. If it is ready to receive, send back the response code 354, otherwise send back other error codes.
  4. The client sends the mail content to the server: Mail content, and sends the end character .
  5. If the mail is received successfully, the response code 250 will be sent back, otherwise other error codes will be sent back.

It should be noted that the real situation will be a little bit more complicated than this, because we have omitted the simple description information behind the authentication process and response code. And the description information of the same response code given by different SMTP servers may be different.

E-mail message format

The message format for e-mail is defined separately in RFC 822. In 2008, this RFC document was updated to RFC 5322.

An e-mail has two parts, the envelope and the content, and the content is composed of the header and the body.

In the first part, we have four keywords: From, To, Cc (cc) and Subject (subject). The mail system will automatically extract the required information and write it on the envelope, and the subject is what the user wants to send.

Characteristics of SMTP protocol

The SMTP protocol has the following characteristics:

  1. The SMTP protocol can only transmit ASCII code text data, and cannot transmit executable files or other binary objects.
  2. The SMTP protocol cannot meet the needs of transmitting multimedia emails (for example, with pictures, audio or video data). Moreover, the characters of other non-English speaking countries (Chinese, Russian, Spanish, etc.) cannot be sent by SMTP.

In order to solve the problem that these texts cannot be transmitted, in 1992, MIME (Multipurpose Internet Mail Extensions) was proposed. MIME is deployed in the process of sending mail to the SMTP server by the sender, and it can convert non-ASCII codes to ASCII codes. It adds 5 new message header fields that provide information about the message body. ; It also defines the format of many email contents, and standardizes the expression method of multimedia email; and defines the transmission code, which can convert any content format and will not be changed by the email system. As an extension, MIME is also used in the HTTP protocol.

Common mail reading protocols

Both POP3 and IMAP4 use a client/server approach based on TCP connections. POP3 uses port 110 and IMAP4 uses port 143.

POP

The full name is Post Office Protocol, and POP3 is its third version. It is an official Internet standard and a very simple, limited-function mail reading protocol. Users can only download mail from the mail server in download and delete mode or download and hold mode.

Internet Mail Access Protocol IMAP

The full name is Internet Mail Access Protocol, and IMAP4 is its fourth version, which is currently only a suggested standard for the Internet.

WWW-based email

You can write, send, receive, and read e-mails by logging in through a browser (providing a user name and password) to the mail server World Wide Web site. Users do not need to install a special user agent program, just use a common World Wide Web browser. At the same time, there is no need to download the mail to the local area for management, and this process can be realized on the mail server website.

Use Telnet client to send mail (Windows)

What is Telnet?
The Telnet protocol is a member of the TCP/IP protocol family, and it is the standard protocol and main method of Internet remote login service. It provides users with the ability to do remote host work on their local computer. Use the telnet program on the end user’s computer to connect to the server. End users can enter commands in the telnet program, and these commands will be run on the server, just as if they were entered directly on the server console. The server can be controlled locally. To start a telnet session, a username and password must be entered to log into the server. Telnet is a commonly used method for remotely controlling a Web server.

Taking Win11 as an example, the following steps are required to enable the Telnet client:

First search the Control Panel, find “Programs” – “Programs and Features” – “Enable or close Windows features”:


Scroll down to find “Telnet Client”, click to check:


Then open cmd and enter telnet to enter the command line:


But we don’t have to do this when we send emails. Let’s go back to cmd, take qq mailbox as an example, enter

telnet smtp.qq.com 25

If you see a 220 response, it is determined that the connection has been successful.

Enter the helo command and wave your hands. The information after helo can be written freely (within the ASCII code range), but it must be written, otherwise an error will be reported:


Seeing 250 here also means that the reception is successful.

Enter auth login, which means login, and return 334 received successfully:


The string of codes in the back can be decoded with a base64 decoding tool to get the username, which is the username, then we enter our own mailbox. Note that you need to transcode again (the email address will not be displayed), otherwise the server will not understand:


Enter your own email password, which also needs to be transcoded:

Note that 535 fail login error is prone to appear here, why? You may take your qq password as smtp password, but it is not. The smtp service of the qq mailbox needs to be manually enabled. You can find “Enable IMAP/SMTP service” through the settings, and get a one-time password after following the prompts.

235 means successful login:


Enter the sender’s email address:

mail from:<[email protected]>

Enter recipient email:

rcpt to:<[email protected]>
//You can fill in multiple

Successful response with 250.

Enter data, enter:

From: <sender's email>
To:<recipient's email>
Subject: <send subject>
Skip a line
<sent content>
. (the input dot represents the end of editing)

250 ok to send mail.

But it is not recommended to send emails like this, why? First of all, you need to send a text message once to get the smtp password, but this is very expensive, sending 0.1 yuan a time, which is very uneconomical, and the packet will be lost if it times out. We generally use the web client to send emails, and the road is simplified, which also reflects the greatness of email management on the World Wide Web, greatly reducing unnecessary time and improving the efficiency of communication.