这是我的 libcurl 代码。我正在尝试将电子邮件发送到我自己的 Linux 电子邮件域。
这是我的示例 libcurl 代码。
curl_easy_setopt(curl, CURLOPT_USERNAME, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b2e283e29353a363e1b36223f34363a323575383436" rel="noreferrer noopener nofollow">[email protected]</a>");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "mypassword");
curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.mydomain.com:25");
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
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_INFILESIZE, file_size);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, fileBuf_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &file_upload_ctx);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //Dont display Curl Connection data Change 1L to 0
res = curl_easy_perform(curl);
当我运行此代码时,出现以下错误。
* Rebuilt URL to: smtp://mail.mydomain.com:25/
* Hostname was NOT found in DNS cache
* Trying <My mail domain Ip address>...
* Connected to mail.mydomain.com (<My mail domain Ip address>) port 25 (#0)
< 220 mail.mydomain.com ESMTP
> EHLO client6
< 250-mail.mydomain.com
< 250-PIPELINING
< 250-SIZE 20480000
< 250-VRFY
< 250-ETRN
< 250-STARTTLS
< 250-AUTH PLAIN LOGIN
< 250-ENHANCEDSTATUSCODES
< 250-8BITMIME
< 250 DSN
> STARTTLS
< 220 2.0.0 Ready to start TLS
* successfully set certificate verify locations:
* CAfile: none
CApath: /etc/ssl/certs
* SSL certificate problem: self signed certificate
* Closing connection 0
curl_easy_perform() failed: Peer certificate cannot be authenticated with given CA certificates
最佳答案
您的问题是您的服务器提供自签名证书,因此curl无法验证其来源。您有多种选择:
最好的选择是获取由知名证书颁发机构签名的服务器证书。有些 CA 会颁发您可以免费使用的证书;搜索“免费 ssl 证书”。您需要能够提供一些证据证明您控制该域。
您可以将自签名证书安装到运行 libcurl 代码的计算机上的受信任 CA 列表中。执行此操作的过程取决于您的操作系统(甚至不同的 Linux 发行版也可能以不同的方式执行此操作)。 This link是 Linux 的一个不错的起点。
您的程序可以告诉 libcurl 使用自签名证书进行验证。请参阅Adding self-signed SSL certificate for libcurl .
您可以 create your own certificate authority并使用前两种方法之一。与自签名相比,它的优点是它将签名和签名证书解耦。如果您想要更改服务器证书(例如,如果它过期或主机名更改),您不一定需要重新配置所有客户端。
为了完整起见,您可以通过将
CURLOPT_SSL_VERIFYPEER
设置为 0 来禁用验证。但是,强烈建议不要这样做,因为它会使访问不安全。您应该仅出于测试目的或在保证客户端和服务器之间的网络安全的极少数情况下执行此操作。
关于email - 尝试使用 libcurl 发送电子邮件时出现证书问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37814590/