C and C # can be used to receive emails in a simple manner,

Source: Internet
Author: User

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}


 

I. C # Implementation Method

C # steps for receiving emails:

1. Need to introduce third-party library (OpenPop. dll Official Website: http://sourceforge.net/projects/hpop/), and add OpenPop reference in the project.

2. Use the Pop3Client class in the OpenPop. dll class library to create a Pop3 server object,

Connect to the server and log on to. host: pop3.163.com port: 110 by default

3. Use the method in the Pop3Client object to receive mail information.

A. GetMessageCount () // get the number of emails

B. GetMessage (int messageNumber) // obtain the content of an email numbered messageNumber.

C. GetMessageSize (int messageNumber) // obtain the size of a message numbered messageNumber.

D. DeleteMessage (int messageNumber) // delete an email numbered messageNumber (only marked and deleted after QUIT)

//...

4. Close the connection and release resources.

 

Some code is as follows:

// Create a PopClient object for connection Authentication

1 using (Pop3Client client = new Pop3Client () 2 {3 Client. connect (host, Pop3_Port, false); 4 5 // account, password, authentication method 6 Client. authenticate (userAddr, password, AuthenticationMethod. auto); // login authentication 7 //... 8}


// Obtain the email information in the mailbox

1 //... 2 int count = client. getMessageCount (); // get the total number of mailboxes 3 int size = client. getMessageSize (1); // get the size of the email numbered 1 4 OpenPop. mime. message msg = client. getMessage (1); // obtain the content of the email with the ID of 1 5 client. deleteMessage (1); // The email marked as 1 is "deleted" 6 7 //...


// Obtain the mail header and Attachment Information

1 //... 2 OpenPop. mime. message msg = client. getMessage (1); 3 4 // obtain information from the Headers member in msg 5 string from = msg. headers. from. address; 6 string SendDate = msg. headers. dateSent. toLocalTime (). toString (); 7 string Subject = msg. headers. subject; 8 string Body = msg. findFirstPlainTextVersion (). getBodyText (); 9 10 // get attachment information 11 List <MessagePart> attachments = msg. findAllAttachments (); 12 int attachCount = attachments. count; 13 if (attachCount> 0) 14 {15 string fileName = attachments [0]. fileName; // attachment name 16 17 // Save the attachment content 18 using (FileStream fs = new FileStream (@ "C: \ 01.txt", FileMode. openOrCreate) 19 {20 attachments [0]. save (fs); 21} 22} 23 //...

 

Reference page:

Http://www.yuanjiaocheng.net/CSharp/Csharp-if-else.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-ternary-operator.html

Http://www.yuanjiaocheng.net/CSharp/Cshartp-switch.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-for-loop.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-while-loop.html

It learning materials

Related Article

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.