使用libcurl進行郵件的下載與轉發

一.郵件下載:

1)相關函數

CURL *curl;

CURLcode res = CURLE_OK;

curl = curl_easy_init();

curl_easy_setopt(curl,CURLOPT_USERNAME,"USER"); //有的郵件服務器不能加域名比如,[email protected],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,"[email protected]");

recipients = curl_slist_append(recipients,"[email protected]");

recipients = curl_slist_append(recipients,"[email protected]"); //抄送

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);




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章