使用libCurl从Gmail POP3帐户读取电子邮件 [英] Reading emails from gmail POP3 account using libCurl

查看:489
本文介绍了使用libCurl从Gmail POP3帐户读取电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你的帮助家伙...我目前工作在一个C + +项目,它必须能够从Gmail邮件POP3帐户,就像标题说的电子邮件。也很重要的说,我需要下载邮件及其正文的附件(是编码base64?)。
事实是,每个人都建议使用libCurl来完成这个任务,但是他们的web代码示例不能正常工作。
我在 Libcurl网站上看到此示例:

I need your help guys... I'm currently working in a C++ project which has to be able to read emails from an gmail POP3 account just like the title says. Also is important to say that i need to download the attachments (is encode base64?) of the mail and its body. The fact is that everyone recommend to use libCurl for this task, but the code sample on their web is not working. I saw this example on 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;
}

正如你看到的,代码看起来很容易通过电子邮件收件箱,但是当我尝试使用 CURLcode curl_easy_perform(CURL *)执行操作时,该函数不返回任何东西,并且进程'死',所以我不能跳过这一行。
我的代码如下:

As you see, the code looks pretty easy for fetching email by email inside the inbox, but when i try to perform the operation with CURLcode curl_easy_perform(CURL *) the function doesn't return anything and the process 'dies' so i can't skip this line. My code is the following one:

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 web, but i really couldn't... Someone could give me a hand with it? :)

推荐答案

我做了家伙!这里有一个很好的例子,你如何访问一个gmail帐户,看看里面是什么。我目前正在研究如何解析里面的信息,因为实际上只检索邮件在收件箱中的数量及其大小。
如果您将网址改为pops://pop.gmail.com:995/1,则会返回邮件的内容,还包括附件的base64编码。谢谢反正...这里是代码! :)

I made it guys! Here a good sample about how you can access to an gmail acount and see what is inside. I'm currently working about how to parse the information inside, because actually only retrieves the amount of mails on inbox and its size. If you change the url as "pops://pop.gmail.com:995/1" it will returns you the content of the messege and also including a base64 encoding for the 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;
}

这篇关于使用libCurl从Gmail POP3帐户读取电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆