Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

c++ - Poco stops after SMTPClientSession.login

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 it besides the one I've encountered so far, but I just started working on it)

    int main(int argc, char** argv)
{
    Poco::Net::SocketAddress add("smtp.gmail.com:465");
    Poco::Net::StreamSocket sock(add);
    Poco::Net::SMTPClientSession sess(sock);
    std::cout << "-";
    sess.login(
            "gmail.com",
            Poco::Net::SMTPClientSession::AUTH_LOGIN,
            "----",
            "----"
    );
    Poco::Net::MailMessage msg;
    Poco::Net::MailRecipient resp(Poco::Net::MailRecipient::PRIMARY_RECIPIENT,"[email protected]");
    msg.addRecipient(resp);
    std::string content("HELP SOS");
    msg.encodeWord(content);
    std::cout << msg.getContent() << "-";
}

When I go into the debugger, it runs fine until it gets to sess.login then suddenly the little bar that represents were I am in the code disappears but the program keeps running (I'm not experienced enough to know what that means). None of the cout stuff I put in actually prints, the debugger just goes past that line but nothing shows up. After a little while this comes up:

terminate called throwing an exception

So what's going on?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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,
                                    "[email protected]");
            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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...