The principle and realization of simple e-mail receiving

Source: Internet
Author: User
Tags sprintf server port

The above article has already talked about how to send a letter, today simply come to an end to talk about how to send a letter!

and the previous process is similar, we also manually simulate a letter process!

in fact, and the previous letter flow is not too much. The same, we take NetEase's mailbox as an example.

we must first connect to NetEase's pop mailbox!

command is: Telnet pop.163.com

The meaning is obvious, requires to connect to NetEase's POP server port 110th.

     

and then you can log in!

input command: User xxxxx (your username, no encryption)

If there is no error, the system will generally return to +ok and things like that.

then enter: Pass XXXXXX (your mailbox password, not encrypted)

The same, if not wrong, the system will generally return to +ok and things like that.

      

now we are ready to operate.

Although there are many commands you can use, there are only two commands that are most commonly used.

The first is the List command, which lists the entries for the message. Let's take a look.

      

indicates that there are 19 messages, and the message size is on the right.

There is another command, naturally, of retr order. It is used to get mail. See my Demo:

       

retr Use rule is RETR + the number of the message you want to get.

all right, then. Now that I have already mentioned it, I would say by the way. Most of the content sent by the server is encrypted with Base64, so we see the full screen of letters. So how do you read out the contents? This is not the focus of this article, so our code to take the way is directly to the server sent over the content of the message into the file, save as a. eml file, and then the mail client can open this file, recommend the use of Foxmail to open this file!

Finally, do not forget to quit the command, shut down the connection to the server, this is no longer demo!

look at the code.

Pop3.cpp

#include <stdio.h> #include <stdlib.h> #include <string.h> #include "pop3.h" cpop3::cpop3 () {Wsadata
	Wsadata;
	WORD Version = Makeword (2, 0);
WSAStartup (version, &wsadata);

} cpop3::~cpop3 () {WSACleanup ();}
	int CPOP3::P op3recv (char* buf, int len, int flags) {/* receive data */INT RS;

	int offset = 0;

		Do {if (offset > len-2) return offset;
		rs = recv (m_sock, buf + offset, len-offset, flags);

		if (Rs < 0) return-1;
		Offset + = RS;
	Buf[offset] = ' the ';

	}while (Strstr (buf, "\r\n.\r\n") = = (char*) NULL);
return offset;		BOOL Cpop3::create (const char* username,//user name Const char* USERPWD,//user Password const char* SVRADDR,
	Server address unsigned short port//service Port {strcpy (m_username, username);
	strcpy (M_userpwd, userpwd);
	strcpy (M_SVRADDR, svraddr);

	M_port = port;
return true;

	BOOL Cpop3::connect () {//create socket M_sock = socket (af_inet, sock_stream, 0);

	IP address char ipaddr[16];
	struct hostent* p; If (P = gethostbyname (m_svraddr) = = NULL)///If no server information is encountered, error {return FALSE; } sprintf (IPAddr, "%u.%u.%u.%u", (unsigned char) p->h_addr_list[0][0], (unsigned char) p->h_addr_list [0]


	[1], (unsigned char) p->h_addr_list[0][2], (unsigned char) p->h_addr_list[0][3]);
	Connect POP server struct sockaddr_in svraddr;
	svraddr.sin_family = af_inet;
	SVRADDR.SIN_ADDR.S_ADDR = inet_addr (ipaddr);
	Svraddr.sin_port = htons (M_port);
	int ret = connect (m_sock, (struct sockaddr*) &svraddr, sizeof (SVRADDR));
	if (ret = = socket_error) {return FALSE;
	//Receive the welcome message from the POP3 server char buf[128];
	int rs = recv (M_sock, buf, sizeof (BUF), 0);

	Buf[rs] = ' the ';
	printf ("%s", buf);
	if (rs <= 0 | | strncmp (BUF, "+ok", 3)!= 0)/* The server failed to return OK.
return TRUE;
	BOOL Cpop3::login () {* * Login/* Send user command/char sendbuf[128];

	Char recvbuf[128];
	sprintf (SendBuf, "USER%s\r\n", m_username);
	printf ("%s", sendbuf); Send (M_sock, SendBuf, strlen) (sendBUF), 0);	Send user name int rs = recv (M_sock, Recvbuf, sizeof (RECVBUF), 0);
	The message sent by the receiving server recvbuf[rs] = ';
	if (rs <= 0 | | strncmp (RECVBUF, "+ok", 3)!= 0)//If no "+ok" indicates failure {return FALSE;
	
	printf ("%s", recvbuf);
	/* Send password information * * memset (sendbuf, 0, sizeof (SENDBUF));
	sprintf (SendBuf, "Pass%s\r\n", m_userpwd);
	Send (M_sock, SendBuf, strlen (SENDBUF), 0);

	printf ("%s", sendbuf);
	rs = recv (m_sock,recvbuf, sizeof (RECVBUF), 0);
	Recvbuf[rs] = ' the ';
	if (rs <= 0 | | strncmp (RECVBUF, "+ok", 3)!= 0) {return FALSE;

	printf ("%s", recvbuf);
return TRUE;
	BOOL Cpop3::list (int& sum) {/* Send List command */char sendbuf[128];

	Char recvbuf[256];
	sprintf (sendbuf, "LIST \ r \ n");
	Send (M_sock, SendBuf, strlen (SENDBUF), 0);

	printf ("%s", sendbuf);
	int rs = recv (M_sock, Recvbuf, sizeof (RECVBUF), 0);
	if (rs <= 0 | | strncmp (RECVBUF, "+ok", 3)!= 0) {return FALSE;
	} Recvbuf[rs] = ';

	printf ("%s", recvbuf); sum = Getmailsum (RECVBUF);	
Get the number of messages return TRUE;BOOL Cpop3::fetchex (int num) {int rs;
	file* FP;
	int flag = 0;
	unsigned int len;

	Char filename[256];
	Char sendbuf[128];

	Char recvbuf[20480];
	/* Send RETR Command * * sprintf (SENDBUF, "RETR%d\r\n", num);
	Send (M_sock, SendBuf, strlen (SENDBUF), 0);
		do {rs = pop3recv (recvbuf, sizeof (RECVBUF), 0)//Receive data if (Rs < 0) {return FALSE;

		} Recvbuf[rs] = '; printf ("Recv RETR Resp:%s", recvbuf);

			Output received data if (flag = = 0) {itoa (num, filename, 10);//rank the file by serial number strcat (filename, ". eml");
			flag = 1;
		fp = fopen (filename, "WB");/Ready to write file} len = strlen (RECVBUF);
		Fwrite (Recvbuf, 1, Len, FP); Fflush (FP);
	
	Refresh}while (Strstr (Recvbuf, "\r\n.\r\n") = = (char*) NULL);
	Fclose (FP);
return TRUE;
	BOOL Cpop3::quit () {char sendbuf[128];

	Char recvbuf[128];
	/* Send QUIT Command * * sprintf (SENDBUF, "quit\r\n");
	Send (M_sock,sendbuf, strlen (SENDBUF), 0);
	int rs = recv (M_sock, Recvbuf, sizeof (RECVBUF), 0);
if (rs <= 0 | | strncmp (RECVBUF, "+ok", 3)!= 0) {		return FALSE;
	} closesocket (M_sock);
return TRUE;
	int cpop3::getmailsum (const char* buf) {int sum = 0; 
	char* p = strstr (buf, "\ r \ n");

	if (p = NULL) return sum;
	p = strstr (P + 2, "\ r \ n");
	
	if (p = NULL) return sum;
	while (P = strstr (P + 2, "\ r \ n"))!= NULL) {sum++;
return sum; }
Pop3.h

#include <WinSock2.h>
#pragma  comment (lib, "Ws2_32.lib")/	* Link ws2_32.lib dynamic link library */

class CPOP3 { Public

:
	CPOP3 ();
	~CPOP3 ();

	Initializes the properties of POP3
	bool Create (const char* username, const char* userpwd, const char* SVRADDR, 
				unsigned short port = 11 0);
	
	Connect POP3 server
	bool Connect ();

	Login Server
	bool login ();

	Use List command to get all the number of messages
	bool list (int& sum);
	
	Get mail
	bool Fetchex (int num = 1) with serial number num;

	Exit command
	bool Quit ();

Protected:
	int getmailsum (const char* BUF);

	SOCKET M_sock;
	Char m_username[32];	/* User name *
	/char m_userpwd[32];		/* Password *
	/char m_svraddr[32];		/* Server domain name * *
	unsigned short m_port;

Private:
	int pop3recv (char* buf, int len, int flags = 0);


then test it with a main program:

main.cpp

#include <stdio.h>
#include "pop3.h"


int main ()
{
	int sum;
	CPOP3 POP3;
	Char username[256] = "it_is_just_a_test@163.com";
	Char password[256] = "19930714LYH";
	Char srv[256] = "pop.163.com";
	POP3. Create (userName, password, SRV, the); 
	

	POP3. Connect (); Connect POP3 server

	POP3. Login ();

	POP3. List (sum);

	if (Sum < 0)
	printf ("You have no letter in box!");

	for (int i = 1; I <= sum; i++) 
	{
		POP3. Fetchex (i);
	}
         
	POP3. Quit ();

	return 0;
}


tested perfectly through VC6 under the test. Then look at your project under the folder, is not there a lot of. eml files. These files can be opened with Foxmail, which is the message received. Try it quickly.

article written here, the proposed receipt of letters, letters have been basically said to understand, in fact, you have a little packaging, you can write a section of MFC mail client code, add a shell.


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.