【发布时间】: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
}
我试图在网上找到一些“清晰”的例子,但我真的找不到...... 有人可以帮我一把吗? :)
【问题讨论】: