尝试使用TLS / SSL发送电子邮件时,为什么会收到Poco :: Net :: SSLException?

时间:2017-04-14 20:36:14

标签: c++ ssl poco

我不知道为什么会收到此错误消息:

terminate called after throwing an instance of 'Poco::Net::SSLException'
  what():  SSL Exception

这是我的代码的相关部分:

#include <Poco/Net/SecureSMTPClientSession.h>
#include <Poco/Net/InvalidCertificateHandler.h>
#include <Poco/Net/AcceptCertificateHandler.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/Net/SecureStreamSocket.h>
#include <Poco/Net/StringPartSource.h>

#include <Poco/Net/MailMessage.h>
#include <Poco/Net/Context.h>
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/SMTPClientSession.h>

#include <string>
#include "smtp.h"

using namespace Poco::Net;
using namespace Poco;
using namespace std;

SMTP::SMTP()
{

    MailMessage mailMessage;

    mailMessage.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, "test", "test"));

    mailMessage.setSubject("test);
    mailMessage.setSender("test");
    mailMessage.setContent("Test\r\n");


    string sSmtpServer = "smtp.gmail.com";
    UInt16 nSmtpPort = 587;
    string sUserName = "test";
    string sPassword = "test";

    initializeSSL();

    SharedPtr<InvalidCertificateHandler> pCert = new AcceptCertificateHandler(false);
    Context::Ptr pContext = new Poco::Net::Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
    SSLManager::instance().initializeClient(0, pCert, pContext);

    SecureStreamSocket* pSSLSocket = new SecureStreamSocket(pContext);
    pSSLSocket->connect(SocketAddress(sSmtpServer, nSmtpPort));
    Poco::Net::SecureSMTPClientSession* pSecure = new SecureSMTPClientSession(*pSSLSocket);
    pSecure->open();



    pSecure->login();
    if (!pSecure->startTLS(pContext))
       throw std::string("Failed to start TLS connection.");


    pSecure->login(SMTPClientSession::AUTH_LOGIN, sUserName, sPassword);
    pSecure->sendMessage(mailMessage);
    pSecure->close();

}

异常发生在以下行:

pSSLSocket->connect(SocketAddress(sSmtpServer, nSmtpPort));

从另一个对象调用SMTP构造函数。我想知道如何更清楚地解决异常问题,我正在使用Qt Creator。

有什么想法吗?感谢。

0 个答案:

没有答案