无法使用libcurl发送电子邮件

时间:2014-05-03 19:50:42

标签: smtp libcurl

我的C ++代码就像这样

CURL *curl;
CURLcode curlres;
struct curl_slist *recipients = NULL;
char from[128] = "<no-reply@mydomain.com>";
char to[128] = "someuser@gmail.com";

curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, COOLCAST_MAILER_URL);

curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
recipients = curl_slist_append(recipients, to);
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, NULL);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curlres = curl_easy_perform(curl);
if(curlres != CURLE_OK)
    qDebug() << "Encountered error in sending email";

屏幕上的输出如下所示:

[ec2-user@ip-10-0-0-51 mailman]$ /path/to/executable
* Rebuilt URL to: smtp://localhost/
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 25 (#0)
< 220 ip-10-0-0-51 ESMTP Sendmail 8.14.4/8.14.4; Sat, 3 May 2014 19:35:57 GMT
> EHLO ip-10-0-0-51
< 250-ip-10-0-0-51 Hello localhost [127.0.0.1], pleased to meet you
< 250-ENHANCEDSTATUSCODES
< 250-PIPELINING
< 250-8BITMIME
< 250-SIZE
< 250-DSN
< 250-ETRN
< 250-DELIVERBY
< 250 HELP
> VRFY someuser@gmail.com
< 252 2.5.2 Cannot VRFY user; try RCPT to attempt delivery (or try finger)
252 2.5.2 Cannot VRFY user; try RCPT to attempt delivery (or try finger)
* Connection #0 to host localhost left intact

现在,我的smtp服务器工作正常。下面是一个telnet会话,我手动执行以下SMTP命令EHLO,MAIL FROM,RCPT TO,DATA,QUIT并成功发送电子邮件

[ec2-user@ip-10-0-0-51 mailman]$ telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 ip-10-0-0-51 ESMTP Sendmail 8.14.4/8.14.4; Sat, 3 May 2014 19:36:41 GMT
MAIL FROM: no-reply@mydomain.com
250 2.1.0 no-reply@mydomain.com... Sender ok
RCPT TO: someuser@gmail.com
250 2.1.5 someuser@gmail.com... Recipient ok
DATA
354 Enter mail, end with "." on a line by itself
This is a test mail 
.
250 2.0.0 s43Jaf12000302 Message accepted for delivery
QUIT
221 2.0.0 ip-10-0-0-51 closing connection
Connection closed by foreign host.

我的libcurl用法中的真正问题似乎是 - 它试图发出VRFY,而我希望它更倾向于使用RCPT TO - 这就是我在telnet会话中所做的。有没有办法在libcurl中设置该选项?或者代码还有其他问题吗?

1 个答案:

答案 0 :(得分:3)

尝试添加

curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

请参阅http://curl.haxx.se/libcurl/c/smtp-mail.html