Following sample works fine with GMAIL:
SmtpSsl::SmtpSsl(QObject *parent) :
QObject(parent) ,
smtp( new QSslSocket ) ,
istd( new QFile ) ,
ostd( new QFile )
{
qDebug() << "constructing";
// QIODevice
QObject::connect( smtp , SIGNAL(aboutToClose()) , this , SLOT(sck_aboutToClose()) );
QObject::connect( smtp , SIGNAL(bytesWritten(qint64)) , this , SLOT (sck_bytesWritten(qint64)) );
QObject::connect( smtp , SIGNAL(readChannelFinished()) , this , SLOT(sck_readChannelFinished()) );
QObject::connect( smtp , SIGNAL(readyRead()) , this , SLOT(sck_readyRead()) );
// QAbstractSocket
QObject::connect( smtp , SIGNAL(connected()) , this , SLOT(sck_connected()) );
QObject::connect( smtp , SIGNAL(disconnected()) , this , SLOT(sck_disconnected()) );
QObject::connect( smtp , SIGNAL(error(QAbstractSocket::SocketError)) , this , SLOT(sck_error(QAbstractSocket::SocketError)) );
QObject::connect( smtp , SIGNAL(hostFound()) , this , SLOT(sck_hostfound()) );
QObject::connect( smtp , SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)) , this , SLOT(sck_proxyAuthRequired()) );
QObject::connect( smtp , SIGNAL(stateChanged(QAbstractSocket::SocketState)) , this , SLOT(sck_stateChanged(QAbstractSocket::SocketState)) );
// QSslSocket
QObject::connect( smtp , SIGNAL(encrypted()) , this , SLOT(sck_encrypted()) );
QObject::connect( smtp , SIGNAL(encryptedBytesWritten(qint64)) , this , SLOT(sck_encryptedBytesWritten(qint64)) );
QObject::connect( smtp , SIGNAL(modeChanged(QSslSocket::SslMode)) , this , SLOT(sck_modeChanged(QSslSocket::SslMode)) );
QObject::connect( smtp , SIGNAL(sslErrors(QList<QSslError>)) , this , SLOT(sck_sslErrors(QList<QSslError>)) );
QObject::connect( smtp , SIGNAL(peerVerifyError(QSslError)) , this , SLOT(sck_peerVerifyError(QSslError)) );
// public part
QObject::connect( smtp , SIGNAL(encrypted()) , this , SLOT(start_session()) );
QObject::connect( smtp , SIGNAL(disconnected()) , this , SLOT(deleteLater()) );
smtp->setPeerVerifyMode( QSslSocket::VerifyPeer );
smtp->connectToHostEncrypted( "smtp.gmail.com" , 465 );
istd->open( stdin , QIODevice::ReadOnly );
ostd->open( stdout , QIODevice::WriteOnly );
qDebug() << "constructed";
}
SmtpSsl::~SmtpSsl()
{
qDebug() << "destroying";
delete smtp;
qDebug() << "destroyed";
}
void
SmtpSsl::start_session()
{
QObject::connect( smtp , SIGNAL(readyRead()) , this , SLOT(receive()) );
}
void SmtpSsl::receive()
{
ostd->write( smtp->readAll() );
ostd->flush();
smtp->write( istd->readLine() );
}
As concerns to your sample. I can guess that you have one of following cases:
- you have connectivity problems at network level. Can you connect smtp.gmail.com port 465 without SSL?
- I believe you have that case. You're intercepting readyRead signal and probably (this code was not shown here) read received data before you've entered encrypted state (i.e. SSL handshake data). Because of you've read handshake data QSslSocket will wait until it can read grabbed handshake data which will result in timeout.
Hope, this help.