You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Poco C++发送Gmail SMTP邮件失败,startTLS相关问题求助

解决Poco C++发送Gmail SMTP邮件失败的问题

核心问题分析

你遇到的startTLS()未定义、SecureSMTPClientSession找不到的问题,本质是缺少SSL相关头文件引用和未链接Poco的SSL依赖库,同时代码里的SMTP端口也有误(Gmail的STARTTLS端口是587,不是487)。

具体修复步骤

1. 补充必要的头文件

要使用SecureSMTPClientSession或startTLS(),必须添加SSL相关头文件:

#include <Poco/Net/SecureSMTPClientSession.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/Net/ConsoleCertificateHandler.h>
#include <Poco/Net/Context.h>

2. 配置VS项目链接库

在VS的项目属性中,找到链接器->输入->附加依赖项,添加以下库(Debug版本后缀加d):

  • PocoNetSSL.lib(Debug:PocoNetSSLd.lib)
  • PocoCrypto.lib(Debug:PocoCryptod.lib)
  • PocoUtil.lib(Debug:PocoUtild.lib)
  • PocoNet.lib(Debug:PocoNetd.lib)
  • PocoFoundation.lib(Debug:PocoFoundationd.lib)

3. 修正代码实现(两种可选方案)

方案一:使用STARTTLS(587端口,推荐)

先建立普通SMTP连接,再升级到TLS加密:

#include <cstdlib>
#include <iostream>
#include <Poco/Net/SMTPClientSession.h>
#include <Poco/Net/MailMessage.h>
#include <Poco/Net/SecureSMTPClientSession.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/Net/ConsoleCertificateHandler.h>
#include <Poco/Net/Context.h>
#include <string>

using namespace Poco::Net;
using namespace std;

int main(int argc, char** argv)
{
    std::string Username = "farhantestingsmtp@gmail.com";
    std::string Password = "cpguxktxoyibiybd";
    
    std::string Receiver = "jattfarhan10@gmail.com";
    std::string Name = "Jatt Farhan";

    std::string Subject = "Hello!";
    std::string Content = "TEXT OF THE EMAIL";

    try
    {
        // 初始化SSL上下文(处理证书验证)
        Poco::SharedPtr<InvalidCertificateHandler> pCertHandler = new ConsoleCertificateHandler(false);
        Context::Ptr pContext = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_RELAXED, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
        SSLManager::instance().initializeClient(0, pCertHandler, pContext);

        MailMessage msg;
        msg.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, Receiver, Name));
        msg.setSender(Username);
        msg.setSubject(Subject);
        msg.setContent(Content);

        // 使用587端口(STARTTLS)
        SMTPClientSession smtp("smtp.gmail.com", 587);
        smtp.open();
        
        // 升级到TLS连接
        smtp.startTLS(pContext);
        
        cout << "Before Login" << endl;
        smtp.login(SMTPClientSession::AUTH_LOGIN, Username, Password);

        cout << "After Login" << endl;
        smtp.sendMessage(msg);
        smtp.close();
        std::cerr << "Sent mail successfully!" << std::endl;
    }
    catch (std::exception& e)
    {
        std::cerr << "Failed to send mail: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

方案二:直接使用SSL连接(465端口)

用SecureSMTPClientSession直接建立SSL加密连接:

#include <cstdlib>
#include <iostream>
#include <Poco/Net/MailMessage.h>
#include <Poco/Net/SecureSMTPClientSession.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/Net/ConsoleCertificateHandler.h>
#include <Poco/Net/Context.h>
#include <string>

using namespace Poco::Net;
using namespace std;

int main(int argc, char** argv)
{
    std::string Username = "farhantestingsmtp@gmail.com";
    std::string Password = "cpguxktxoyibiybd";
    
    std::string Receiver = "jattfarhan10@gmail.com";
    std::string Name = "Jatt Farhan";

    std::string Subject = "Hello!";
    std::string Content = "TEXT OF THE EMAIL";

    try
    {
        // 初始化SSL上下文
        Poco::SharedPtr<InvalidCertificateHandler> pCertHandler = new ConsoleCertificateHandler(false);
        Context::Ptr pContext = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_RELAXED, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
        SSLManager::instance().initializeClient(0, pCertHandler, pContext);

        MailMessage msg;
        msg.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, Receiver, Name));
        msg.setSender(Username);
        msg.setSubject(Subject);
        msg.setContent(Content);

        // 直接用465端口的SecureSMTPClientSession
        SecureSMTPClientSession smtp("smtp.gmail.com", 465);
        smtp.open();
        
        cout << "Before Login" << endl;
        smtp.login(SMTPClientSession::AUTH_LOGIN, Username, Password);

        cout << "After Login" << endl;
        smtp.sendMessage(msg);
        smtp.close();
        std::cerr << "Sent mail successfully!" << std::endl;
    }
    catch (std::exception& e)
    {
        std::cerr << "Failed to send mail: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

额外注意事项

  • Gmail要求启用低安全性应用访问(或使用App密码,若开启了2FA),否则会拒绝登录请求。
  • 代码中的端口号务必正确:587对应STARTTLS,465对应SSL直连,不要用错误的487端口。

内容的提问来源于stack exchange,提问作者Farhan Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 00:10:20