Winsock POP3 tutorial (1)

Source: Internet
Author: User
Learn about POP3

POP3 Introduction
POP3 is short for Post Office Protocol-version 3. This Protocol is currently used by most email programs, the email client can log on to the email server to receive emails. POP3 is an offline Model Protocol. After the client queries and downloads an email, the client is disconnected from the server. You can delete or modify the content of the email on the client.

POP3 Communication Method
Initially, the mail server starts the POP3 service by listening to TCP port 110. When the customer program needs to use the service, it will establish a TCP connection with the mail server. After the connection is established, the client can communicate with the server. The POP3 client communicates with the server using the ASCII command line, the client sends a POP3 command (the POP3 command is case-insensitive) to the user. The POP3 command consists of a command and some parameters. All commands end with a CRLF pair. Commands and parameters are composed of printable ASCII characters separated by spaces. The command generally contains three to four letters, but each parameter can be up to 40 characters long. When receiving a response, the mail server also sends an ASCII Response Message to the client.
A POP3 response consists of a status code and a command that may contain additional information. All responses are also ended by the CRLF pair. There are two status codes: "OK" ("+ OK") and "failed" ("-Err ") (The "+ OK" or "-Err" returned by the server must be in uppercase ).
The email server can return single or multiple lines of text. For multiple lines of text, no line is ended with a carriage return line break (CRLF, if the returned response contains the "CRLF. CRLF "(where ". the ASCII code 46) indicates that the response returned by the server has ended.
In the lifecycle, POP3 sessions have several different states. Once the TCP connection is opened and the email server sends a confirmation message, the process enters the authorization status. In this status, the customer must confirm with the mail server that he is the customer. Once the authentication succeeds, the mail server obtains the resources related to the customer's mail, and the process enters the "transaction" status. In this status, the customer initiates a service request. When the customer issues a quit command, the process enters the "Update" status. In this status, the mail server releases the resources obtained in the "operation" status, sends a message, and terminates the connection.
As an offline Model Protocol, the mail server must have an automatic disconnection timer, which can be at least 10 minutes. After the time limit is exceeded, the mail server will not go to the "Update" status, but directly disconnect the TCP connection, and will not modify the messages on the server, nor send any response message to the client.

Simple POP3 example
The following uses a simple example to demonstrate how to use the Winsock Control to log on to the mail server and then exit through the POP3 protocol.
Create a new project, add a textbox to it, set its name to txtmsg, set the multiline attribute to true, and resize the text box as much as possible. Then, add four commandbutton controls and set their names to connect, Loggin, cmdlist, and quit respectively. They are used to connect, log on, mail list, and exit the mail server. Add a Winsock Control, set the Protocol attribute to 0-scktcpprotocol, and set the remotehost attribute to the address of the POP3 server, for example, pop.163.com. Set remoteport to the POP3 Service port of the server, generally, it is 110. Set localport to an unused port, for example, 410.
The Code is as follows:

Private sub branch connect_click ()
'Connect to the email server
Winsock1.connect
End sub

Private sub register list_click ()
'List emails on the email server
Winsock1.senddata "list" & vbcrlf
End sub

Private sub register loggin_click ()
'Log On to the email server
Winsock1.senddata "User XXXXX" & vbcrlf
Winsock1.senddata "Pass AAAAA" & vbcrlf
End sub

Private sub queue quit_click ()
'Send and Exit message
Winsock1.senddata "quit" & vbcrlf
End sub

Private sub winsock1_close ()
'Close the connection to the server
Winsock1.close
End sub

Private sub winsock1_connect ()
Debug. Print "connection"
End sub

Private sub winsock1_dataarrival (byval bytestotal as long)
Dim s as string

'Get the Server Response Information
S = space (bytestotal)
Winsock1.getdata s
Txtmsg. Text = txtmsg. Text & "message:" & S
End sub

Private sub winsock1_error (byval number as integer, description as string, byval scode as long, byval source as string, byval

Helpfile as string, byval helpcontext as long, canceldisplay as Boolean)
'Output error message
Debug. Print description
Debug. Print number
End sub

To run the code above, you need to set the code above:

Winsock1.senddata "User XXXXX" & vbcrlf
Winsock1.senddata "Pass AAAAA" & vbcrlf

Set XXXXX to the username used to log on to the POP3 server, and set AAAAA to the password used to log on to the POP3 server.

Run the program, click Login connect to the POP3 server, click Login Loggin to log on to the mail server, click cmdlist to list the emails on the server, and click Login quit to log out. If the POP3 address is set correctly and the user name and password are correct, the following content should be listed in txtmsg:

Now let's look at the above Code. In the code above, click Login Loggin to log on to the mail server. After Successful Logon, the mail server will return a message similar to the following: + OK POP3 Welcome to quarkmail server version 1.2.1. The status is "authenticated". In this status, the client needs to send a logon command to log on to the email server. Here we send the "user" and "pass" commands to log on, after successful login, the mail server will return a successful response of + OK; otherwise, an error message of-err will be returned.
After successful login, the status is "processing". Then, you can process the emails on the mail server by sending commands such as "list.
After the email is processed, click Cancel quit to send the "quit" command to log out. After the email is successfully exited, the status changes to "Update" and the email server disconnects the TCP connection, the Winsock client receives the message to close the connection. Therefore, you must close the TCP connection of the client in the Winsock Close event.
In POP3 protocol, the mail server must respond to commands sent by the client, whether valid or invalid. In our program, we only need to get the server response message in the dataarrival event of WinSock and process the message according to the POP3 protocol to understand what the mail server is saying to us ". As for the detailed explanation of the commands sent to the mail server and the meaning of the message returned by the mail server, the tutorial will introduce them gradually in the future.

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.