【问题标题】:Reading emails from gmail POP3 account using libCurl使用 libCurl 从 gmail POP3 帐户读取电子邮件
【发布时间】:2013-11-17 16:38:27
【问题描述】:

我目前在一个 C++ 项目中工作,该项目必须能够读取来自 gmail POP3 帐户的电子邮件,就像标题所说的那样。同样重要的是,我需要下载邮件及其正文的附件(编码 base64?)。 事实上,每个人都建议使用 libCurl 来完成这项任务,但他们网站上的代码示例无法正常工作。 我在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 *) 执行操作时,该函数不会返回任何内容并且过程“死亡”,所以我不能跳过这一行。 我的代码如下:

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

}

我试图在网上找到一些“清晰”的例子,但我真的找不到...... 有人可以帮我一把吗? :)

【问题讨论】:

    标签: c++ libcurl pop3


    【解决方案1】:

    伙计们,我成功了!这是一个很好的示例,介绍了如何访问 gmail 帐户并查看其中的内容。我目前正在解析里面的信息,因为实际上它只检索收件箱中的邮件数量及其大小。 如果您将 url 更改为 "pops://pop.gmail.com:995/1" 它将返回消息的内容,并且还包括附件的 base64 编码。无论如何,谢谢...这是代码! :)

    #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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 2012-03-30
      • 2013-03-24
      相关资源
      最近更新 更多