C ++ libcurl 使用 GitHub 库发送 SMTP 邮件失败

c++ libcurl sent smtp mail using github library fails

本文关键字:SMTP 失败 libcurl 使用 GitHub      更新时间:2023-10-16

我正在尝试使用此 git 存储库中给出的代码。代码不是我的,我要感谢honeyligo的工作!

不幸的是,我无法正确运行它(我不得不解决一些编译问题,但没什么大不了的(。

将主.cpp设置为

#include <unistd.h>
#include "curlsmtp.h"
int main(){
CurlSmtp* mail = new CurlSmtp(
"MYADDRESS@gmail.com",
"MYPASSWORD",
{ "foo@yahoo.fr" },
{},
{},
{},
"it's a subject",
"hello world you aredfsf",
"smtp.gmail.com",
"465");
mail->send_mail();
sleep(3);
delete mail;
return 0;
}

它不断返回

* Expire in 200 ms for 1 (transfer 0x558a3b95d7e0)

然后在超时后失败。代码大约有 500 行长,注释很好,但我无法弄清楚它为什么会失败。我对 curl 了解不多,但我正在寻找一种自动发送带有 pdf 的邮件的方法,并附在我的高中生身上。我需要实现一个 c++ 解决方案,因为这将是一个更大项目的一部分。

感谢您的帮助!

在从CLI 和使用 libcurl 的 C 应用程序中使用 curl 进行一些测试后,我发现了一个问题:如果您明确使用 smtps (smtps://smtp.gmail.com(,gmail 只接受 smtp 请求。 由于它在这个curlsmtp lib中硬编码以使用smtp://,因此您可以修改它(set_curl_option方法(或直接使用libcurl(这里有一个简单的例子:https://curl.haxx.se/libcurl/c/smtp-mime.html(

此外,您必须在Google帐户中设置2FA并创建应用密码,或者如果没有2FA(https://myaccount.google.com/lesssecureapps(则为安全性较低的应用启用访问权限。