一.邮件下载:
1)相关函数
CURL *curl;
CURLcode res = CURLE_OK;
curl = curl_easy_init();
curl_easy_setopt(curl,CURLOPT_USERNAME,"USER"); //有的邮件服务器不能加域名比如,test@qq.com,qq.com不能要
curl_easy_setopt(curl,CURLOPT_PASSWORD,"123456");
curl_easy_setopt(curl,CURLOPT_URL,"pop3://test.com:110/1");
//110是默认端口,如果使用其他端口要跟着变,后面的1代表第几份邮件
curl_easy_setopt(curl,CURLOPT_WRITEDATA,指针);
//指针,指示写入数据需要存储的地方,如文件或队列
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,,write_data);
//回调函数,上面的指针就是给其传参数的作用,回调函数格式 static size_t write_data(coid *ptr,size_t size, size_t nmemb, void *stream);
stream就是上面的指针,ptr指向已经接受的数据,中间两个相乘代表字节数
res = curl_easy_perform(curl);
if(res!=CURLE_OK)
fprintf(stderr,"%s",curl_easy_strerror(res));
curl_easy_cleanup(curl);
二.发送邮件
1)struct curl_slist *recipients=NULL;
curl = curl_easy_init();
curl_easy_setopt(curl,CURLOPT_URL,"smtp://to.com:25/from.com"); //默认端口25,可不写,其他端口必须写
curl_easy_setopt(curl,CURLOPT_MAIL_FROM,"user@from.com");
recipients = curl_slist_append(recipients,"user@to.com");
recipients = curl_slist_append(recipients,"CCuser@to.com");
//抄送
curl_easy_setopt(curl,CURLOPT_MAIL_RCPT,recipients);
curl_easy_setopt(curl,CURLOPT_READDATA,指针);
//指针,指示写入数据需要存储的地方,如文件或队列
curl_easy_setopt(curl,CURLOPT_READFUNCTION,read_data);
curl_easy_setopt(curl,CURLOPT_UPLOAD,1L);
// TRUE to prepare for an upload 设置非零值
res = curl_easy_perform(curl);
if(res!=CURLE_OK)
fprintf(stderr,"%s",curl_easy_strerror(res));
curl_easy_cleanup(curl);
使用cURL发送与接收邮件
15万+

被折叠的 条评论
为什么被折叠?



