libcurl 库的使用方法

首先要安装这个库,

其次,要把需要的协议加上,./configure --enable-smtp --enable-pop3

make

make install

使用curl --version 查看使用版本是否一样



smtp代码实例:

###############################################################################

#include <stdio.h>
#include <curl/curl.h>

size_t read_data(void *ptr, size_t size, size_t nmemb, void *data)
{
// FILE *fp = (FILE *)data;
size_t return_size = fread(ptr, size, nmemb, data);
printf("write %d\n", (int)return_size);
return return_size;

}
int main()
{
CURL *curl;
CURLcode res;

FILE *fp = fopen("data.txt", "rb");
if (fp == NULL) {
printf("can't open \n");
return -1;
}
struct curl_slist *slist=NULL;

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
curl_easy_setopt(curl, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_SSL);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "<mulinhai123@163.com>"); //发送者
curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.163.com");
slist = curl_slist_append(slist, "<9191247@163.com>"); //接收者
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, slist);
curl_easy_setopt(curl, CURLOPT_USERNAME, username);
curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
curl_easy_setopt(curl, CURLOPT_READDATA, fp);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_data);

}
res = curl_easy_perform(curl);

printf("The return code is %d\n", res);

fclose(fp);
curl_slist_free_all(slist);
curl_easy_cleanup(curl);

return 0;
}

data.txt内容可以随便

subject: test

123

 

转自:http://blog.sina.com.cn/s/blog_6026371d0100mqab.html 

posted @ 2011-08-16 10:48  hnrainll  阅读(1714)  评论(0编辑  收藏  举报