【问题标题】:I cannot send an email using curl and c++我无法使用 curl 和 C++ 发送电子邮件
【发布时间】:2021-09-20 17:09:51
【问题描述】:

我尝试使用 curl 和 c++ 发送电子邮件,就像 this example 一样,但是当我执行程序时:

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


int main()
{   
    int a;
    char errbuf[CURL_ERROR_SIZE] = {0};
    CURL *curl = curl_easy_init();
    CURLcode res;
    struct upload_status upload_ctx = { 0 };
    if(curl)
    {   
        curl_easy_setopt(curl, CURLOPT_URL, "smtps://smtp.gmail.com:465");
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "");
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "");
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, "");
        
        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_CAINFO, "D:\\MinGW\\cacert.pem");
        curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }
    check_errors(res, errbuf);

    return 0;
}


我得到这个错误:

curl_easy_perform() failed: Failed sending data to the peer

libcurl: (55) MAIL failed: 530

我删除了大部分代码,因为我无法发布问题,但其余的都在示例中。

【问题讨论】:

  • 530 错误表示邮件服务器无法对您进行身份验证。
  • 在过去(在垃圾邮件之前),SMTP 服务器是信任的,允许任何人连接和发送邮件。如今(在现代世界中)SMTP 服务器被严格锁定(您不希望您的 SMTP 服务器被垃圾邮件发送者滥用),因此该示例不适用于任何商业 SMTP 服务器。如果您设置自己的 SMTP 服务器(docker 映像),您可能会侥幸成功,但您的 SMTP 服务器将无法发送任何外发邮件(因为没有其他服务器会信任您)。如果你想发送电子邮件,最好的办法是学习 google gmail API(它使用 REST 而不是 SMTP)。
  • 我已经能够发送电子邮件,但它实际上被标记为垃圾邮件。
  • 请在下面发表关于您所做的事情的答案,以便其他人可以从您的问题中学习。

标签: c++ email curl


【解决方案1】:

我在代码中遇到的问题是我认为通过使用: curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "myemail@example.com");我指定了用户名,实际上是一个错误。

要指定用户名 libcurl 有另一个选项: curl_easy_setopt(curl, CURLOPT_USERNAME, "myemail@example.com");

【讨论】:

    猜你喜欢
    • 2015-08-10
    • 2013-01-21
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2015-08-19
    • 2015-06-11
    相关资源
    最近更新 更多