c - 使用 libCurl 构建 SMTP 客户端

标签 c curl libcurl smtpclient

我最近开始使用libcurl库构建一个简单的SMTP客户端,可以将邮件发送到gmail smtp服务器。基本代码与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 的新手,对它不是很熟悉。谢谢。

最佳答案

在第一个示例中,curl 尝试发送邮件。 MAIL FROM: 命令是发送新邮件时 SMTP 中的第一个命令。它告诉接收服务器信封发件人是谁。发送邮件的尝试被拒绝,因为 GMAil 服务器拒绝接受未加密的电子邮件(STARTTLS 是一个有效将纯文本 SMTP session 转换为 SSL 加密 session 的命令)。

在第二种情况下,curl 并没有尝试发送电子邮件,而是只是尝试验证 xxxx@gmail.com 是真实的电子邮件地址。 Gmail 的响应方式是不以某种方式实际告诉curl 答案。由于垃圾邮件,大多数 SMTP 服务器不再对 VRFY 做出有用的响应。我的猜测是,关闭上传选项相当于要求curl不要发送任何内容,只需验证地址即可。

您需要做的是将curl配置为对SMTP使用TLS,这可能涉及一些证书的配置和处理,但使用curl来做到这一点超出了我的知识水平。

编辑

谷歌搜索一下就会发现 example of using TLS在curl 网站上。

编辑2

以及来自 somebody who got it working with gmail 的链接.

关于c - 使用 libCurl 构建 SMTP 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30307283/

相关文章:

发送 .tar.gz 文件时出现 curl 错误 56 "Failure when receiving data from the peer"

rest - Keycloak 管理 REST API : query more than 100 resource objects

c++ - 如何使用curlpp的InfoGetter?

ssl - 使用自定义构建的 libcurl+openssl 时,Nginx 的 https SSL 证书验证失败,但适用于系统curl

c - 关于在c中使用fgets和scanf读取输入的问题

c - 有没有办法在C中读取括号内以逗号分隔的2个整数,忽略空格

c++ - 幂运算符C语言程序

使用 libcurl 固定 SSL 证书

c++ - curl 回调未返回相同数量的字节

C 语言国际象棋游戏 - 有些棋子不动