Poco在SMTPClientSession.login之后停止 [英] Poco stops after SMTPClientSession.login

查看:202
本文介绍了Poco在SMTPClientSession.login之后停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚从Poco图书馆开始,试图创建一个电子邮件程序(我几乎没有任何知道)。以下是我的代码(除了迄今为止遇到的问题之外,还可能存在其他问题,但我刚刚开始工作)

 code> 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,michaelrgoldfine@gmail.com);
msg.addRecipient(resp);
std :: string content(HELP SOS);
msg.encodeWord(content);
std :: cout<<< msg.getContent()<<< - ;
}

当我进入调试器,它运行正常,直到得到sess。登录然后突然显示代表的小酒吧我在代码中消失,但程序不断运行(我没有足够的经验知道这是什么意思)。没有一个cout的东西,我放在实际打印,调试器只是超过那一行,但没有显示。稍后一段时间:

 终止调用抛出异常

那么发生了什么?

解决方案

使用SMTP over TLS(端口465传递给 SocketAddress )。一来,您必须学习(1)POCO中的TLS和证书处理,然后重点关注(2)您的目标:发送电子邮件。



我建议开始用更简单的例子学习POCO。您可以在POCO源代码中的各种样本目录中找到示例代码。



我认为您的代码是只是挂在TLS握手上,因为它不知道该怎么做。



这些是你应该在之前做的修复解决方案:


  1. 将您的代码放在 try / catch 块中。 POCO使用例外。

  2. StreamSocket 替换为 SecureStreamSocket

  3. 正确初始化<$ c $的最简单的方法c> SecureStreamSocket 通过应用程序类。请参阅应用程序幻灯片和 Util / samples / SampleApp / src / SampleApp.cpp

  4. 请参阅 SSLManager 如何正确地告诉应用程序哪些证书使用。

  5. 不要指定一个主机名到 login()方法。主机名是可选的,应该是客户端主机名,而不是服务器端(见SMTP RFC)。

  6. 记住实际发送消息!您的代码不发送: - )

确定,现在运行代码。我作为一个练习离开了第4步和第6步,但是这个代码至少会运行TLS握手,会告诉你它不能验证服务器的证书,如果你在终端上回答是,证书上的问题就会失败SMTP验证。

  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收件人(Poco :: Net :: MailRecipient :: PRIMARY_RECIPIENT,
michaelrgoldfine@gmail.com);
msg.addRecipient(recipient);
字符串内容(HELP SOS);
msg.encodeWord(content);
} catch(Poco :: Exception& e){
cout<< 错误:<< e.displayText()<< ENDL;
return -1;
}
return 0;
}
};

POCO_APP_MAIN(MiniApp)


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,"michaelrgoldfine@gmail.com");
    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?

解决方案

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)

这篇关于Poco在SMTPClientSession.login之后停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆