Program for receiving emails using POP3 protocol and Socket

Source: Internet
Author: User
Tags socket error

I. First, let's take a look at the POP3 protocol:

POP3 (Post Office Protocol 3) is the 3rd version of the Post Office Protocol. It specifies how a personal computer connects to the mail server on the Internet to send and receive mail. It is the first offline protocol standard for Internet email. POP3 allows you to store emails on a server to a local host (your computer, at the same time, emails are deleted or saved on the mail server according to client operations, while the POP3 server is a mail server that follows the POP3 protocol to receive emails. POP3 is a member of the TCP/IP protocol family.
1939. POP3 default port: 110

The POP3 client sends a command to the POP3 server and waits for a response. The POP3 command adopts the command line format and uses ASCII code. A server response is composed of a single command line or multiple command lines. the first line of response is in ASCII text + OK or-ERR (OK indicates success, and-ERR indicates failure) indicates whether the operation is successful or failed.

Ii. POP3 command code

Command Parameter status description

USER username recognizes that if this command is successful with the following pass Command, status conversion will occur.

PASS password approval

APOPName, Digest recognizes that Digest is an MD5 message Digest

------------------------------------------

STATNone processes the mailbox statistics sent back by the request server, such as the total number of emails and total bytes.

UIDL [Msg #] indicates the unique identifier of the Response Message. Each identifier of a POP3 session will be unique.

LIST [Msg #] Number of returned mails processed and size of each email

RETR [Msg #] process and return all text of the email identified by Parameters

DELE [Msg #] The processing server marks the emails marked by parameters as deleted and is executed by the quit command.

RSETNone server resets all emails marked as deleted, used to cancel the DELE Command

TOP [Msg #] The processing server will return the content of the first n lines of emails identified by parameters. n must be a positive integer.

NOOPNone processing server returns a positive response

------------------------------------------

QUITNone update

A. The client wants to end the session

B. If the server is in the 'process' status, it will enter the 'update' status to delete any emails marked as deleted

C. Changes from processing status to update status and return to approval status

D. If the server is in the 'approval' status when the command is issued, the session ends and the 'update' status is not performed.

Iii. Programming implementation:

The Code is as follows:

// ================================================ ========================================#Include <iostream. h> # include <winsock2.h> # define MAXBUF 2048 # pragma comment (lib, "ws2_32.lib ") // ================================================ =========== bool InitSock (); char sendbuf [MAXBUF] = {0}; char name [] = "user abcde@163.com \ t \ n "; // pop3 Request Header char pass [] = "pass 123456 \ t \ n"; char ret [] = "retr 1 \ t \ n "; char quit [] = "quit \ t \ n"; // ch Ar recvbuf [MAXBUF] = {0}; int num = 0; // ================================================ ======================================= void main () {InitSock (); struct hostent * phost = gethostbyname ("pop3.163.com"); // struct hostent * phost = gethostbyname ("pop.qq.com "); // note that pop3.163.com or pop.163.com can be used for mail servers such as 163. The ping results are the same. // ----- qq mail ------------- pop. qq. comSOCKET httpsock = socket (AF_INET, SOCK_STREAM, IPPROTO_TC P); if (INVALID_SOCKET = httpsock) {cout <"socket error! "<Endl; WSACleanup ();} SOCKADDR_IN sock_addr = {0}; sock_addr.sin_family = AF_INET; records = htons (110); // records = inet_addr (" records "); // obtain the IP address memcpy (& images, phost-> h_addr_list [0], phost-> h_length); cout <"POP3 server IP:" <inet_ntoa (sock_addr.sin_addr) <endl; int res = connect (httpsock, (sockaddr *) & sock_addr, sizeo F (sockaddr); if (res = SOCKET_ERROR) {cout <"connect error! "<Endl; closesocket (httpsock); WSACleanup (); return;} recv (httpsock, recvbuf, MAXBUF, 0); cout <" Client: connect successfully! \ NServer: "<recvbuf <endl; memset (recvbuf, 0, MAXBUF); send (httpsock, name, strlen (name), 0 ); // -------------------------- NAMErecv (httpsock, recvbuf, MAXBUF, 0); cout <"Client: send name \ nServer:" <recvbuf <endl; memset (recvbuf, 0, MAXBUF); send (httpsock, pass, strlen (pass), 0); // -------------------------- PASSWORDrecv (httpsock, recvbuf, MAXBUF, 0); cout <"Client: send passwo Rd \ nServer: "<recvbuf <endl; memset (recvbuf, 0, MAXBUF); send (httpsock," stat \ r \ n ", strlen ("stat \ r \ n"), 0); recv (httpsock, recvbuf, MAXBUF, 0); cout <"Client: send stat \ nServer: "<recvbuf <endl; memset (recvbuf, 0, MAXBUF); // Sleep (20); send (httpsock, ret, strlen (ret), 0 ); cout <"Client: send retr 1 \ n"; while (1) {num = recv (httpsock, recvbuf, MAXBUF, 0); cout <"Server: "<recvb Uf <endl; memset (recvbuf, 0, MAXBUF); if (num <= 0) {break ;}// send (httpsock, "dele 1 ", strlen ("dele 1"), 0); send (httpsock, quit, strlen (quit), 0); num = recv (httpsock, recvbuf, MAXBUF, 0 ); cout <"Client: send quit \ nServer:" <recvbuf <endl; memset (recvbuf, 0, MAXBUF); // Sleep (20); closesocket (httpsock ); system ("pause"); WSACleanup ();} // ================================================ ============== ================== Bool InitSock () {WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD (2, 2 ); err = WSAStartup (wVersionRequested, & wsaData); if (err! = 0) {return false;} if (LOBYTE (wsaData. wVersion )! = 2 | HIBYTE (wsaData. wVersion )! = 2) {WSACleanup (); return false;} return true ;}

(The account and password in the name and pass Arrays can be modified according to your actual situation)

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.