C and C # can be used to receive emails in a simple manner,
The main content of this article is to receive simple emails in two ways, one is through the C language, the other is through the C,
The two methods have many differences in implementation, but they are essentially the same.
I. C language implementation
Steps for receiving emails in C language:
1. Create a TCP connection port: 110 by default
2. Connection to Pop3 server name: for example, "pop3.163.com"
3. authenticated login user account and password
4. send the request and receive the server information. send (...) and return "+ OK"
A. Get the number of emails in the mailbox "STAT \ r \ n" back "+ OK 10 ..."
B. Get the size of each email "LIST \ r \ n"
C. Obtain the first n rows of data of a specified email. "TOP [nMsg] [nLine]"
NMsg: nMsg
NLine: The first nLine line.
D. Get the specified email message content "RETR [nMsg] \ r \ n"
The implementation code is as follows:
// Create a TCP connection
1 if ((connection = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)2 {3 return INVALID_SOCKET;4 }
// Connect to the Pop3 server
1 void ConnectToServer () 2 {3 // set the SockAddr_In address structure 4 SOCKADDR_IN serAddr; 5 serAddr. sin_family = AF_INET; 6 serAddr. sin_port = htons (POP3_PORT);/* POP3_PORT = 110 */
7 // serverName = "POP3.163.com ";
8 serAddr. Records = inet_addr (serverName); 9 if (serAddr. Records = INADDR_NONE) 10 {11 LPHOSTENT lphost; 12 lphost = gethostbyname (serverName); 13 if (lphost! = NULL) 14 {15 serAddr. sin_addr.s_addr = (LPIN_ADDR) lphost-> h_addr)-> S_un.S_addr; 16} 17 else18 {19 WSASetLastError (WSAEINVAL); 20 return; 21} 22 // connect 23 connect (connection, (SOCKADDR *) & serAddr, sizeof (serAddr) 24 return; 25 26}
// Receive data
// Obtain the number of emails in the mailbox
1 // get the number of emails 2 int GetNumEmail (SOCKET connection) 3 {4 char sStat [10] = {"STAT \ r \ n "}; 5 char response_buf [RESPONSE_BUFFER_SIZE] = {0}; 6 7 send (connection, sStat, strlen (sStat), 0); 8 if (recv (connection, response_buf, RESPONSE_BUFFER_SIZE, 0) = SOCKET_ERROR) 9 return-1; 10 11 char szResponse [20] = {0}; 12 strncpy (szResponse, response_buf, 3); 13 if (strcmp (szResponse, "+ OK") = 0) // return the correct message content "+ OK 10... "14 return GetNumFromBuf (response_buf); 15 return 0; 16}
// Obtain the size of each email
1 // get the size of each Mail 2 BOOL GetList (SOCKET connection) 3 {4 char szList [100] = {0}; 5 sprintf (szList, "LIST \ r \ n"); 6 7 char response_buf [RESPONSE_BUFFER_SIZE] = {0}; 8 send (connection, szList, strlen (szList), 0 ); 9 if (recv (connection, response_buf, RESPONSE_BUFFER_SIZE, 0) = SOCKET_ERROR) 10 return FALSE; 11 12 printf ("receive data: % s", response_buf ); // return data 13 return TRUE; 14}
// Obtain the first n rows of data of the specified email
1 // obtain the first n rows of data of the specified email. 2 BOOL GetTop (SOCKET connection, int nMsg, int nLine) 3 {4 char szTop [100] = {0 }; 5 sprintf (szTop, "TOP % d \ r \ n", nMsg, nLine); 6 7 char response_buf [RESPONSE_BUFFER_SIZE] = {0}; 8 send (connection, szTop, strlen (szTop), 0); 9 Sleep (nLine * 20); // latency, waiting for the data to be fully received 10 if (recv (connection, response_buf, RESPONSE_BUFFER_SIZE, 0) = SOCKET_ERROR) 11 return FALSE; 12 13 printf ("Receive Data: % s \ n", response_buf); 14 return TRUE; 15}
// Obtain the content of the specified email
1 // get the content of the specified email 2 BOOL GetEmail (SOCKET connection, int nMsg, char * msg) 3 {4 char sRetr [100] = {0}; 5 sprintf (sRetr, "RETR % d \ r \ n", nMsg); 6 7 char response_buf [RESPONSE_BUFFER_SIZE * 10] = {0}; 8 send (connection, sRetr, strlen (sRetr ), 0); 9 Sleep (1000); // a latency must be added so that data can be fully received 10 if (recv (connection, response_buf, RESPONSE_BUFFER_SIZE * 10, 0) = SOCKET_ERROR) 11 return FALSE; 12 13 printf ("Receive Data: % s \ n", response_buf); 14 return TRUE; 15}