0

I don't know why i get this error message:

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

This is the relevant part of my code:

#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();

}

The exception happens at the line:

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

The SMTP constructor is called from another object. I wonder how I could troubleshoot the exception more clearly, I am using Qt Creator.

Any ideas? Thanks.

Sergey Kolesnik
  • 3,009
  • 1
  • 8
  • 28
cppnewbie
  • 1
  • 1
  • Can you provide the complete call stack and the arguments you're passing to `SocketAddress(sSmtpServer, nSmtpPort)`? – Eduard Malakhov Apr 14 '17 at 21:20
  • I added the full code. – cppnewbie Apr 15 '17 at 05:37
  • I see, it is because of the port. When I change the port to 443 it does not give the exception anymore. However, the service is not listening on that port. So there is something wrong. I can put any port but not the ones where gmail listens. – cppnewbie Apr 15 '17 at 05:55
  • Shouldn't it be port 465? https://support.google.com/mail/answer/7126229?hl=en – Eduard Malakhov Apr 15 '17 at 06:14
  • Yes for SSL. I am not sure if this code is configured for SSL or TLS. However the method startTLS() won't work. I noticed this: http://stackoverflow.com/questions/17281669/using-smtp-gmail-and-starttls – cppnewbie Apr 15 '17 at 07:07
  • And what is the complete exception message you're getting? – Eduard Malakhov Apr 15 '17 at 07:12
  • And what do you get if you change the port number to 465 in your code, leaving everything else as is? – Eduard Malakhov Apr 15 '17 at 07:13
  • If I change to 465 it works but not with startTLS() function then. So it works with SSL but not with TLS. I get this message to the running window and no more messages. I guess I go just with SSL and not use startTLS. Not sure if Gmail even supports it tho. – cppnewbie Apr 15 '17 at 11:49

0 Answers0