Read emails from your Gmail POP3 account using libCurl

I need your guys ... I'm currently working on a C ++ project that should be able to read emails from a Gmail Gmail account, as the header says. It is also important to say that I need to download attachments (is it encode base64?) Of the mail and its body. The fact is that everyone recommends using libCurl for this task, but the sample code on their Internet does not work. I saw this example on the Libcurl website :

#include <stdio.h> #include <curl/curl.h> int main(void) { CURL *curl; CURLcode res; curl = curl_easy_init(); if(curl) { /* Set username and password */ curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password"); /* This will only fetch the message with ID "1" of the given mailbox */ curl_easy_setopt(curl, CURLOPT_URL, "pop3s:// user@pop.example.com /1"); /* Perform the request, res will get the return code */ res = curl_easy_perform(curl); /* Check for errors */ if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); /* always cleanup */ curl_easy_cleanup(curl); } return 0; } 

As you can see, the code looks pretty easy to receive email by email inside the Inbox, but when I try to perform a operation with CURLcode curl_easy_perform (CURL *) , the function returns nothing and the process dies, so I can’t miss this line. My code is as follows:

 void MailServer::Open(char *username,char *password) { m_username = username; //username = example@gmail.com m_password = password; //password = blabla123 curl = curl_easy_init(); curl_easy_setopt(curl,CURLOPT_USERNAME,username); curl_easy_setopt(curl,CURLOPT_PASSWORD,password); m_popsAccount = "pop3s://" + m_username +"/1"; curl_easy_setopt(curl, CURLOPT_URL, m_popsAccount.c_str()); res = curl_easy_perform(curl); //here does not return anything } 

I tried to find some “clear” example on the Internet, but I really couldn’t ... Can someone give me a hand? :)

+6
source share
1 answer

I made it guys! Here is a good example of how you can access your gmail account and see what's inside. I am currently working on how to analyze the information inside, because it actually only extracts the number of letters per mailbox and its size. If you change the url as "pops: //pop.gmail.com: 995/1" , it will return the contents of the messege and will also contain base64 encoding for attachments. Thanks, anyway ... here is the code! :)

 #pragma region Types struct MemoryStruct { char *memory; size_t size; }; #pragma endregion void MailServer::Open(char *username,char *password) { m_username = username; m_password = password; struct MemoryStruct chunk; chunk.memory = (char*) malloc(1); //crecerá según sea necesario con el realloc chunk.size = 0; //no hay datos en este punto //inicializacion curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); //login curl_easy_setopt(curl,CURLOPT_USERNAME,username); curl_easy_setopt(curl,CURLOPT_PASSWORD,password); m_popsAccount = "pop3s://pop.gmail.com:995/"; curl_easy_setopt(curl, CURLOPT_URL, m_popsAccount.c_str()); curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); //some servers needs this validation curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); res = curl_easy_perform(curl); if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } else { /* here is where you can work with the data inside the chunk... */ printf("%s\n",chunk.memory); //here is the information printf("%lu bytes retrieved\n", (long)chunk.size); } //se libera la memoria si hay datos if(chunk.memory) free(chunk.memory); /* always cleanup */ curl_global_cleanup(); } size_t MailServer::WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) { size_t realsize = size * nmemb; struct MemoryStruct *mem = (struct MemoryStruct *)userp; mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1); if(mem->memory == NULL) { /* out of memory! */ printf("not enough memory (realloc returned NULL)\n"); return 0; } memcpy(&(mem->memory[mem->size]), contents, realsize); mem->size += realsize; mem->memory[mem->size] = 0; return realsize; } 
+8
source

Source: https://habr.com/ru/post/957501/


All Articles