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
samplesdirectories 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:
try/catchblock. POCO uses exceptions.StreamSocketwithSecureStreamSocket.SecureStreamSocketis via theApplicationclass. See the Applications slides andUtil/samples/SampleApp/src/SampleApp.cpp.SSLManagerfor how to properly tell the Application which certificates to use.login()method. The hostname is optional and should be the client hostname, not the server (See the SMTP RFC).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.