I'm looking for an example on how to use the STARTTLS with SMTP that is mentioned here http://developer.appinf.com/?p=60
Could someone please crank up a few lines to demonstrate?
Thanks...
guenter wrote:Not that simple, actually. You first have to create a plain socket connection (like SMTPClientSession does now), then issue a STARTTLS command, and then initiate a SSL/TLS connection over the existing socket connection. This requires some changes in the SMTPClientSession class - at least an additional protected member function that allows you to change the underlying socket to a SecureStreamSocket.
void connectTLS()
{
std::string response;
//...
std::auto_ptr<DialogSocket> nonSecureSocket;
nonSecureSocket.reset(new DialogSocket());
nonSecureSocket->connect(address);
int status = nonSecureSocket->receiveMessage(response);
sendEHLO(response);
status = sendCommand("STARTTLS", response);
if (!isPositiveCompletion(status))
throw SMTPException("The TLS is unavailable", response);
Poco::Net::Context* ptrContext = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE );
/**
* Creating SSL socket over existing non secure socket
*/
Poco::Net::SecureStreamSocket secureSocket(ptrContext);
secureSocket = secureSocket.attach(*nonSecureSocket_release(),secureSocket.context());
/**
* Assingning created socket to SMTPClientSession
* @note EHLO and authentication should be done manually (not by calling session_::login())
*/
session_.reset(new Poco::Net::SMTPClientSession(secureSocket));
/**
* Sending EHLO
*/
sendEHLO(response);
/**
* Authenticating
*/
authenticate(response);
//...
}
void sendEHLO(std::string& response)
{
int status = sendCommand("EHLO", getHost(), response);
if (isPermanentNegative(status))
status = sendCommand("HELO", getHost(), response);
if (!isPositiveCompletion(status)) throw SMTPException("EHLO/HELO send failed", response);
}
int sendCommand(const std::string& command, std::string& response)
{
if (session_.get())
{
return session->sendCommand(command,response);
}
else
{
if (!nonSecureSocket_get())
return -1;
nonSecureSocket->sendMessage(command);
return nonSecureSocket->receiveStatusMessage(response);
}
}
authenticate() like other methods like sendEHLO() may be copy pasted from poco sources.
SecureSMTPClientSession session("smtp.gmail.com", 465);
session.login();
session.STARTTLS();
session.login("smtp.gmail.com", Poco::Net::SecureSMTPClientSession::AUTH_LOGIN, "gmail_username", "gmail_password"); Users browsing this forum: No registered users and 2 guests