我最近开始使用libcurl库来构建一个简单的SMTP客户端,可以将邮件发送到gmail smtp server.Base代码与来自http://curl.haxx.se/libcurl/c/smtp-tls.html的smtp-tls.c相同,但它对我不起作用。当我编译以下代码时
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
static const char *payload_text[] = {
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
"To: " "<xxxx@gmail.com>" "\r\n",
"From: " "<xxxx@gmail.com>" "(Example User)\r\n",
"Message-ID: <dcd7cb36-11db-487a-9f3a- e652a9458efd@rfcpedant.example.org>\r\n",
"Subject: SMTP TLS example message\r\n",
"\r\n", /* empty line to divide headers from body, see RFC5322 */
"The body of the message starts here.\r\n",
"\r\n",
"It could be a lot of lines, could be MIME encoded, whatever.\r\n",
"Check RFC5322.\r\n",
NULL
};
struct upload_status {
int lines_read;
};
static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
{
struct upload_status *upload_ctx = (struct upload_status *)userp;
const char *data;
if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
return 0;
}
data = payload_text[upload_ctx->lines_read];
if(data) {
size_t len = strlen(data);
memcpy(ptr, data, len);
upload_ctx->lines_read++;
return len;
}
return 0;
}
int main(void)
{
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx;
upload_ctx.lines_read = 0;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_USERNAME, "xxxx@gmail.com");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxxx");
curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:587");
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM,"xxxx@gmail.com");
recipients = curl_slist_append(recipients, "xxxx@gmail.com");
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}
return (int)res;
}
输出
./smtp: /usr/local/lib/libcurl.so.4: no version information available (required by ./smtp)
* Rebuilt URL to: smtp://smtp.gmail.com:587/
* Trying 74.125.195.109...
* Connected to smtp.gmail.com (74.125.195.109) port 587 (#0)
< 220 mx.google.com ESMTP h5sm17332853wjn.20 - gsmtp
> EHLO alan-EasyNote-TS11HR
< 250-mx.google.com at your service, [109.175.102.154]
< 250-SIZE 35882577
< 250-8BITMIME
< 250-STARTTLS
< 250-ENHANCEDSTATUSCODES
< 250-PIPELINING
< 250-CHUNKING
< 250 SMTPUTF8
> MAIL FROM:<xxxx@gmail.com>
< 530 5.7.0 Must issue a STARTTLS command first. h5sm17332853wjn.20 - gsmtp
* MAIL failed: 530
> QUIT
< 221 2.0.0 closing connection h5sm17332853wjn.20 - gsmtp
* Closing connection 0
curl_easy_perform() failed: Failed sending data to the peer
,但是当我评论curl_easy_setopt(curl,CURLOPT_UPLOAD,1L)时;我得到不同的输出
./smtp: /usr/local/lib/libcurl.so.4: no version information available (required by ./smtp)
* Rebuilt URL to: smtp://smtp.gmail.com:587/
* Trying 74.125.136.108...
* Connected to smtp.gmail.com (74.125.136.108) port 587 (#0)
< 220 mx.google.com ESMTP g8sm17345920wjn.18 - gsmtp
> EHLO alan-EasyNote-TS11HR
< 250-mx.google.com at your service, [109.175.102.154]
< 250-SIZE 35882577
< 250-8BITMIME
< 250-STARTTLS
< 250-ENHANCEDSTATUSCODES
< 250-PIPELINING
< 250-CHUNKING
< 250 SMTPUTF8
> VRFY xxxx@gmail.com
< 252 2.1.5 Send some mail, I'll try my best g8sm17345920wjn.18 - gsmtp
252 2.1.5 Send some mail, I'll try my best g8sm17345920wjn.18 - gsmtp
* Connection #0 to host smtp.gmail.com left intact
所以任何人都可以告诉我这段代码有什么问题,我应该怎么做才能更改(添加或删除)成功发送邮件。我必须说我是libcurl的新手并且不熟悉它。谢谢。
答案 0 :(得分:1)
在第一个示例中,curl尝试发送邮件。 lines2
命令是SMTP发送新消息时的第一个命令。它告诉接收服务器信封发件人是谁。拒绝发送邮件的尝试是因为GMAil服务器拒绝接受未加密的电子邮件(STARTTLS是一个有效地将纯文本SMTP会话转换为SSL加密会话的命令)。
在第二种情况下,curl不是尝试发送电子邮件,而只是尝试验证MAIL FROM:
是否是真实的电子邮件地址。 Gmail做出回应并没有真正告诉卷曲这种或那种方式的答案。由于垃圾邮件,大多数SMTP服务器不再对VRFY做出有效响应。我的猜测是,关闭上传选项相当于要求curl不发送任何内容,只需验证地址。
您需要做的是配置curl以使用TLS进行SMTP,这可能涉及一些证书的配置和处理,但使用curl执行此操作超出了我的知识水平。
修改强>
谷歌网站上的一些谷歌搜索结果显示example of using TLS。
修改2