gpt4 book ai didi

c++ - 使用 libcurl 和 C++ 将从 IMAP 服务器下载的电子邮件保存到文件中

转载 作者:行者123 更新时间:2023-11-30 04:02:49 32 4
gpt4 key购买 nike

我可以从 IMAP 服务器获取电子邮件,但无法将它们保存到计算机上的文件中以供以后处理。有没有办法使用 libcurl 在 C++ 中执行此操作?

    #include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = CURLE_OK;

curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "username");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "password");

curl_easy_setopt(curl, CURLOPT_URL, "imaps://imap.gmail.com/INBOX/;UID=1");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

/* Perform the fetch */
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 (int)res;
}

最佳答案

您正在执行数据传输:

res = curl_easy_perform(curl);

但是您没有对这些数据做任何事情。您必须设置一个回调函数,只要 curl_easy_perform(curl) 返回 CURLE_OK 就会被调用。

这是libcurl tutorial说:

Let's assume for a while that you want to receive data as the URL identifies a remote resource you want to get here. Since you write a sort of application that needs this transfer, I assume that you would like to get the data passed to you directly instead of simply getting it passed to stdout. So, you write your own function that matches this prototype:

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);

You tell libcurl to pass all data to this function by issuing a function similar to this:

curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, write_data);

关于c++ - 使用 libcurl 和 C++ 将从 IMAP 服务器下载的电子邮件保存到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24852274/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com