最近,我开始使用libcurl库来构建一个简单的SMTP客户端,它可以将邮件发送到gmail smtp server.Base代码与来自http://curl.haxx.se/libcurl/c/smtp-tls.html的smtp-tls.c相同,但是它不适用于http://curl.haxx.se/libcurl/c/smtp-tls.html,我编译了以下代码。
#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并不熟悉,也不太熟悉it.Thanks。
发布于 2015-05-18 16:01:18
在第一个例子中,curl试图发送邮件。当要发送新邮件时,MAIL FROM:命令是SMTP中的第一个命令。它告诉接收服务器谁是信封发送者。发送邮件的尝试被拒绝,因为GMAil服务器拒绝接受未加密的电子邮件(STARTTLS是一个有效地将纯文本SMTP会话转换为SSL加密会话的命令)。
在第二种情况下,curl不是试图发送电子邮件,而是简单地试图验证xxxx@gmail.com是一个真正的电子邮件地址。Gmail的回应实际上并不是以这样或那样的方式告诉curl答案。由于垃圾邮件,大多数SMTP服务器对VRFY不再有帮助。我的猜测是,关闭上传选项相当于要求curl不要发送任何内容,只需验证地址。
您需要做的是将curl配置为将TLS用于SMTP,这可能涉及到一些证书的配置和处理,但使用curl进行配置和处理超出了我的知识范围。
编辑
一点点谷歌会在卷发网站上找到一个使用TLS的示例。
编辑2
还有来自和gmail一起工作的人的链接。
https://stackoverflow.com/questions/30307283
复制相似问题