Poco stops after SMTPClientSession.login

前端 未结 2 535
别那么骄傲
别那么骄傲 2020-12-21 15:02

I just started with the Poco library and tried to create an email program (Which I knew virtually nothing about). The following is my code (There may be other problems with

相关标签:
2条回答
  • 2020-12-21 15:30

    Yes, so I struggled with login(), trying to use smtp.gmail.com. This is the excerpt of the communication with the SSL session that made it work.

    string host("smtp.gmail.com")
    Poco::UInt16 port = 587;
    
    SecureSMTPClientSession session(host, port);
    
    session.open();
    
    Poco::Net::initializeSSL();
    
    SharedPtr<InvalidCertificateHandler> ptrHandler = new AcceptCertificateHandler(false);
    
    Context::Ptr ptrContext = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_RELAXED, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
    
    SSLManager::instance().initializeClient(0, ptrHandler, ptrContext);
    
    try
    {
      // SSL
      session.login();
      if (session.startTLS(ptrContext))
      {
        session.login(SMTPClientSession::AUTH_LOGIN, "user@gmail.com", "yourpassword");
        session.sendMessage(message);
      }
      session.close();
      Poco::Net::uninitializeSSL();
    }
    catch (SMTPException &e)
    {
      cout << e.message() << endl;
      session.close();
      Poco::Net::uninitializeSSL();
      return 0;
    }
    

    Original source:

    http://www.axistasoft.sg/tutorials/cpp/poco/item/sending-email-messages-using-smtp-protocol

    0 讨论(0)
  • 2020-12-21 15:31

    You are attempting to use SMTP over TLS (the port 465 passed to the SocketAddress). In one shot you have to learn (1) TLS and certificate handling in POCO, before focusing on (2) your goal: sending an email message.

    I suggest to start learning POCO with simpler examples. You can find sample code in the various samples directories in the POCO source code.

    I think that your code is just hanging on the TLS handshake, because it doesn't know what to do.

    These are the fixes you should do before looking at the solution:

    1. Place your code inside a try/catch block. POCO uses exceptions.
    2. Replace StreamSocket with SecureStreamSocket.
    3. The simplest way to properly initialize SecureStreamSocket is via the Application class. See the Applications slides and Util/samples/SampleApp/src/SampleApp.cpp.
    4. See the documentation for the SSLManager for how to properly tell the Application which certificates to use.
    5. Don't specify an hostname to the login() method. The hostname is optional and should be the client hostname, not the server (See the SMTP RFC).
    6. Remember to actually send the message! Your code is not sending it :-)

    OK, and now for the running code. I left steps 4 and 6 as an exercise, but this code will at least run the TLS handshake, will tell you that it cannot verify the server's certificate and, if you answer Yes on the terminal to the questions on the certificates, it will fail the SMTP authentication.

    class MiniApp : public Poco::Util::Application {
        int main(const vector <string>& args) {
            try {
                Poco::Net::SocketAddress add("smtp.gmail.com:465");
                Poco::Net::SecureStreamSocket sock(add);
                Poco::Net::SMTPClientSession session(sock);
                session.login(Poco::Net::SMTPClientSession::AUTH_LOGIN, "user", "pw");
                Poco::Net::MailMessage msg;
                Poco::Net::MailRecipient recipient(Poco::Net::MailRecipient::PRIMARY_RECIPIENT,
                                        "michaelrgoldfine@gmail.com");
                msg.addRecipient(recipient);
                string content("HELP SOS");
                msg.encodeWord(content);
            } catch (Poco::Exception& e) {
                cout << "Error: " << e.displayText() << endl;
                return -1;
            }
            return 0;
        }
    };
    
    POCO_APP_MAIN(MiniApp)
    
    0 讨论(0)
提交回复
热议问题