Simple implementation of SMTP protocol and POP3 protocol

Source: Internet
Author: User

Objective

This article mainly introduces the principle of SMTP and POP3 protocol, and then attaches the simple encapsulation and implementation of it.

The RFC documents corresponding to the SMTP protocol are: RFC821

SMTP protocol

The SMTP (Simple Mail Transfer Protocol) is simply the message Transfer Protocol, which is a set of rules for sending mail from the source address to the destination, and it controls the way the letters are relayed. The SMTP protocol belongs to the TCP/IP protocol family, which helps each computer find its next destination when sending or relaying letters. Through the server specified by the SMTP protocol, e-Mail can be sent to the recipient's server, the whole process only a few minutes. An SMTP server is a sending mail server that follows the SMTP protocol and is used to send or relay e-mail messages. (Excerpt from Baidu Encyclopedia)

Send mail procedure for SMTP protocol

When a user sends a message, the message is sent to the sender's SMTP server by the SMTP protocol, which is then sent by the sender's SMTP server to the recipient's SMTP mail server, and the message is then downloaded from the receiving server by the recipient through the POP3 protocol.

For example, User A ([email protected]) sends a message to User B ([email protected]), and the message is sent first to the sending SMTP server, which is smtp.126.com. Then from smtp.126.com to smtp.qq.com, and finally to [email protected].

Through the above description, I believe that you have a general understanding of the mail sending and receiving process. So next, let's analyze the SMTP protocol.

The SMTP protocol defines the port of the SMTP server, the connection process, and the process of sending mail, as long as we follow the steps set forth in the Protocol.

First, we need to use the socket to connect to the SMTP port, the default port is 25. We can use Telnet to connect the tests. We enter Telnet smtp.126.com 25来 connect to NetEase's 126 mailbox server on the command line

After the socket connection is established, the server returns to us the welcome message, as shown in:

Next we will send the user name and password information to the server to log on to the mailbox server. one thing to be aware of here is that SMTP The protocol is string-based, so our username and password are passed base64 encoding conversion so that there is no illegal character problem, I will give the Base64 in the code later encoding and decoding the implementation code .

Here we give you an online base64 code conversion URL, you can transcode the above after the conversion of the Code (http://tools.jb51.net/tools/base64_decode-gb2312.php).

The commands for logging into the mail server are:

EHLO smtp.126.com\r\n ( description: \ r \ n for newline carriage return operation)

AUTH login\r\n ( Description: Login command)

bactnt55a==\r\n ( Description of Base64 encoded user name)

mtizywrmytey\r\n ( Description of Base64 encoded password)

Next we don't use the Mail from:<[email protected]> command to tell where the message came from. Then use the RCPT to:<[email protected]> command to tell whom, RCPT to can only be followed by multiple RCPT to indicate to multiple receivers. Finally, use the data command to transfer the encoded message content.

In the SMTP protocol, each command ends with \ r \ n, and the length is less than 76, and more than a portion is placed into a command. When using data to send a command, one line per 76 characters until all data is sent. When all the mail data is sent, we need to tell the mail server that we have finished sending the data. The SMTP protocol stipulates that, after sending the data, we send a \r\n.\r\n to indicate that the data has been sent, indicating the following: s on behalf of the sender, R for the recipient, which note <CRLF> representative \ r \ n

S:data

r:354 Start mail input; End With <CRLF>.<CRLF>

S:blah blah blah ...

S: ... etc. etc.

S: <CRLF>.<CRLF>

r:250 OK

Simple implementation of SMTP protocol and POP3 protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.