How to receive e-mail (POP3 protocol introduction)

Source: Internet
Author: User
Tags contains end header mail connect requires string client

The INTERNET,POP3 (POSTOFFICEPROTOCOL3) protocol is commonly used to receive e-mail messages. This protocol is easy to learn because it contains only 12 commands (3 of which you can completely ignore their existence). These commands are used by the client computer to send to the remote server. Conversely, the server returns two response codes to the client computer. If you have not read this site another article-"Winsockterminal sample program", please read the article first, because you need that program to do the experiment.

Run the sample program and click the ConnectTo button on the toolbar. In the pop-up dialog box, enter the address of the mail server you want to connect to, for example:

Mail.comset.net

Select the POP3 protocol, and then click the Connect button. If it succeeds (if there is a problem with your server address, don't be suspicious of the program), you will see the information that the server returns to you in the text box. The answer to my server is as follows:

+okpop3mail.comset.netv5.49serverready

This string tells you that the machine was successful. Different servers respond differently, but the beginning is the same:

"+ok". It means that the server accepts the command order and syntax used by the client computer. If the beginning is "-err", you are using the wrong command. In your application, you only need to handle the first few characters from the server's response. Such a small amount of processing is precisely the advantage of POP3 relative to other protocols.

Authorization status

OK, now that we've established a connection to the server and got a positive answer from the server. The next thing to do is to tell the server your identity. It is not difficult for the server to manage thousands of mailboxes. It has established an account for each mailbox. Each account is comprised of a username and password. When you connect to the server, the first command to send is user, and the username is the parameter of the command. The specific wording is as follows:

Userusername

If the username is registered, the server returns a correct message, and then you can enter the following command to tell the server your password.

Passpassword

If the username and password match, the server will also return a correct message telling you that you can go to the next stage, that is, transactionstate.

Transaction state

To find out how many emails are currently in your mailbox and the size of those messages, use the Stat command. It will tell you the current status of the mailbox. The information returned by the server may be the following:

+ok00 (no mail in your email)

+ok3825 (you have three messages in your mailbox, size is 825 eight bytes)

If there is no mail in the mailbox, there is nothing to do, use the QUIT command to end the connection with the server. If you have mail in your mailbox, you can download it to your own machine using the RETR command. This command requires the index of the message as its parameter. For example, if we want to receive the first message, you can enter the following command:

RETR1

To retrieve other messages, use the command above to add the corresponding message number. If you enter a message number that exceeds the number of messages in the mailbox, the server returns a "-err" to you. As a response to the RETR command, the server returns you a string. This string contains the following things: "+ok", message size, message text, and a string that contains a decimal point. The last string, which is the decimal point, indicates the end of the message. For example:

Client:retr2

Server:+ok234octets

server:< Message text >

Server:.

Note that the process of receiving messages above does not mean transferring these messages to your computer. You are getting a copy of these emails and still keep them in your inbox until you delete them with the DELE command. Similarly, the DELE command also requires you to provide the index number of the message. In fact, when you use the DELE command, these messages are not immediately removed from the server. These messages are only made with a deletion tag, and when you disconnect from the server, the server is not actually deleted. If you want to remove these deletion tags, use the rset command. This command does not target a particular message, but removes all deletion tags, so you do not need to provide an email index number to this command.

In addition to the commands mentioned above, when you are in the transaction state, POP3 also supports two other commands. The two commands are: List and noop. The list command can be used to list information about a message. As shown below:

Client:list

Server:+ok2messages (320octets)

server:1120

server:2200

Server:.

By providing the message index number to the list command, you can list information for a specific message, as in the following example:

Client:list2

server:+ok2200

NOOP command specific things do not do, just to check the connection with the server.

Update status

When you are in the update state, you have only one command available, that is: QUIT. Use this command to confirm any changes you have made, delete the flagged messages, and close the connection.

Other POP3 commands

The POP3 agreement also supports three additional commands: Top, uidl, and Apop. Only the first command is presented here. The top command can be used to obtain a message header and a predefined string. Typically, this command is especially useful if you have a large e-mail message in your mailbox. With the top command, you can display only the message header information from which to find the subject of the mail author. You can even download only this part of the information. The syntax for this command is as follows:

Topmsgn

The index number of the msg-message

n the number of rows you want to display



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.