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:
- Place your code inside a
try/catch
block. POCO uses exceptions.
- Replace
StreamSocket
with SecureStreamSocket
.
- The simplest way to properly initialize
SecureStreamSocket
is via the Application
class. See the Applications slides and Util/samples/SampleApp/src/SampleApp.cpp
.
- See the documentation for the
SSLManager
for how to properly tell the Application which certificates to use.
- 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).
- 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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…