简体   繁体   中英

How can I receive emails in C++ via POP3?

I've been trying to find a POP3 C++ client on the internet but I haven't found any luck.

We are working on a project for school that is essentially a C++ course (so I can't use C#...), and we are making a Email client that has to support sending and receiving emails and attachments. We are also working with .NET (because apparently MFC is terrible, although I haven't used it, anyone have an opinion on this?), and so I would prefer a Microsoft built in library solution to this. So far we've been able to get sending of email working using SMPTclient, but no POP3 luck.

If anyone has a solution in .NET that would be great, otherwise I'll have to write my own POP3 client code, and if anyone has a link for that to get me in the right direction it would be much appreciated.

POCO在其网络库中具有POP3和SMTP支持。

I've written SMTP e-mail sending code using COM-CDO (on Windows); for getting email (POP3) I condensed the following code snippet as a watered-down example. If your POP3 server uses TLS/SSL, your hosed without 3rd-party software - you have to implement SSL and the code would probably take years to develop. I second the use of POCO. Here's what I have for non-SSL (sorry for any Z DIrectory calls, it should be easy to figure out):

#include <winsock2.h>

#define MAX_MSG         40000                   // max message length
#define COMM_SUFF       "\r\n"
#define COMM_USER       "USER "
#define COMM_PASS       "PASS "
#define COMM_STAT       "STAT\r\n"
#define COMM_LIST       "LIST\r\n"
#define COMM_QUIT       "QUIT\r\n"
#define COMM_RETR       "RETR "
#define COMM_TOP        "TOP "
#define COMM_DELETE     "DELE "
#define COMM_NOOP       "NOOP\r\n"

SOCKET sock_no;                             // SOCKET no
struct sockaddr_in sok = {0};               // SOCKET struct
struct hostent *hp;
string_o command, s, msg_num;
string_o user = "EMAIL_USERNAME";
string_o passw = "EMAIL_PASSWORD";
char buff[MAX_MSG];                         // pop-3 output buffer
sock_no = socket (AF_INET, SOCK_STREAM, 0); // init socket
sok.sin_family = AF_INET;
int n_bytes = 0;                            // num of return bytes

s = "gorth@Vettrasoft.com";
hp = gethostbyname(s.data());             // get server IP address
if (hp != NULL)
    bcopy ((u_char *) &sok.sin_addr, (const u_char *) hp->h_addr, hp->h_length);
else
    return -1;

sok.sin_port = (unsigned short int) htons(110);

//..........................................................
// connect to pop-3 server and logon
//..........................................................
if (::connect(sock_no, (sockaddr *) &sok, sizeof (sockaddr_in)) != 0)
    return -1;

n_bytes = ::recv (sock_no, buff, MAX_MSG, 0); // skip first server answer

// send "user" command
command = COMM_USER + user + COMM_SUFF;
n_bytes = ::send (sock_no, command.data(), command.size(), 0);

// get server answer
n_bytes = ::recv (sock_no, buff, MAX_MSG, 0);

// send PASS command
command = COMM_PASS + passw + COMM_SUFF;
n_bytes = ::send (sock_no, command.data(), command.size(), 0);

n_bytes = ::recv (sock_no, buff, MAX_MSG, 0); // get server answer

//..........................................................
// get list of messages; get number of messages in inbox
//..........................................................
command = COMM_STAT;
n_bytes = ::send (sock_no, command.data(), command.size(), 0);
n_bytes = ::recv (sock_no, buff, MAX_MSG, 0);

// extract number of messages
msg_num = get_field(buff, "+OK ");
pos = msg_num.find(' ', &ie);
msg_num = msg_num.substring(0, pos);
count = ::z_str_to_int (msg_num.data());

//..........................................................
// do all messages from list. # of messages begins from 1
//..........................................................
for (i = 1; i <= count && !ie; i++)
    ie = process_nextmail (sock_no, i, buff, do_kill);

//..........................................................
// quit and disconnect pop-3 server; send "QUIT" command
//..........................................................
command = COMM_QUIT;
n_bytes = ::send (sock_no, command.data(), command.size(), 0);
return ::closesocket(sock_no);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM