Added support for TLS connection (STARTTLS).
Added getters for responseCode, responseText and socket.
This commit is contained in:
parent
67a337ddaa
commit
1560ab819f
@ -24,21 +24,13 @@
|
|||||||
|
|
||||||
/* [1] Constructors and destructors */
|
/* [1] Constructors and destructors */
|
||||||
|
|
||||||
SmtpClient::SmtpClient(const QString & host, int port, ConnectionType ct) :
|
SmtpClient::SmtpClient(const QString & host, int port, ConnectionType connectionType) :
|
||||||
name("localhost"),
|
name("localhost"),
|
||||||
authMethod(AuthPlain),
|
authMethod(AuthPlain),
|
||||||
connectionTimeout(5000),
|
connectionTimeout(5000),
|
||||||
responseTimeout(5000)
|
responseTimeout(5000)
|
||||||
{
|
{
|
||||||
if (ct == TcpConnection)
|
setConnectionType(connectionType);
|
||||||
this->useSsl = false;
|
|
||||||
else if (ct == SslConnection)
|
|
||||||
this->useSsl = true;
|
|
||||||
|
|
||||||
if (useSsl == false)
|
|
||||||
socket = new QTcpSocket(this);
|
|
||||||
else
|
|
||||||
socket = new QSslSocket(this);
|
|
||||||
|
|
||||||
this->host = host;
|
this->host = host;
|
||||||
this->port = port;
|
this->port = port;
|
||||||
@ -85,15 +77,17 @@ void SmtpClient::setPort(int port)
|
|||||||
|
|
||||||
void SmtpClient::setConnectionType(ConnectionType ct)
|
void SmtpClient::setConnectionType(ConnectionType ct)
|
||||||
{
|
{
|
||||||
if (ct == TcpConnection)
|
this->connectionType = ct;
|
||||||
this->useSsl = false;
|
|
||||||
else if (ct == SslConnection)
|
|
||||||
this->useSsl = true;
|
|
||||||
|
|
||||||
if (useSsl == false)
|
switch (connectionType)
|
||||||
|
{
|
||||||
|
case TcpConnection:
|
||||||
socket = new QTcpSocket(this);
|
socket = new QTcpSocket(this);
|
||||||
else
|
break;
|
||||||
|
case SslConnection:
|
||||||
|
case TlsConnection:
|
||||||
socket = new QSslSocket(this);
|
socket = new QSslSocket(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString& SmtpClient::getHost() const
|
const QString& SmtpClient::getHost() const
|
||||||
@ -123,10 +117,7 @@ int SmtpClient::getPort() const
|
|||||||
|
|
||||||
SmtpClient::ConnectionType SmtpClient::getConnectionType() const
|
SmtpClient::ConnectionType SmtpClient::getConnectionType() const
|
||||||
{
|
{
|
||||||
if (useSsl)
|
return connectionType;
|
||||||
return SslConnection;
|
|
||||||
else
|
|
||||||
return TcpConnection;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString& SmtpClient::getName() const
|
const QString& SmtpClient::getName() const
|
||||||
@ -139,6 +130,20 @@ void SmtpClient::setName(const QString &name)
|
|||||||
this->name = name;
|
this->name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const QString & SmtpClient::getResponseText() const
|
||||||
|
{
|
||||||
|
return responseText;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SmtpClient::getResponseCode() const
|
||||||
|
{
|
||||||
|
return responseCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
QTcpSocket* SmtpClient::getSocket() {
|
||||||
|
return socket;
|
||||||
|
}
|
||||||
|
|
||||||
/* [2] --- */
|
/* [2] --- */
|
||||||
|
|
||||||
|
|
||||||
@ -146,10 +151,17 @@ void SmtpClient::setName(const QString &name)
|
|||||||
|
|
||||||
bool SmtpClient::connectToHost()
|
bool SmtpClient::connectToHost()
|
||||||
{
|
{
|
||||||
if (useSsl)
|
switch (connectionType)
|
||||||
((QSslSocket*) socket)->connectToHostEncrypted(host, port);
|
{
|
||||||
else
|
case TlsConnection:
|
||||||
|
case TcpConnection:
|
||||||
socket->connectToHost(host, port);
|
socket->connectToHost(host, port);
|
||||||
|
break;
|
||||||
|
case SslConnection:
|
||||||
|
((QSslSocket*) socket)->connectToHostEncrypted(host, port);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Tries to connect to server
|
// Tries to connect to server
|
||||||
if (!socket->waitForConnected(connectionTimeout))
|
if (!socket->waitForConnected(connectionTimeout))
|
||||||
@ -179,11 +191,44 @@ bool SmtpClient::connectToHost()
|
|||||||
waitForResponse();
|
waitForResponse();
|
||||||
|
|
||||||
// The response code needs to be 250.
|
// The response code needs to be 250.
|
||||||
if (responseCode != 250)
|
if (responseCode != 250) {
|
||||||
{
|
|
||||||
emit smtpError(ServerError);
|
emit smtpError(ServerError);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (connectionType == TlsConnection) {
|
||||||
|
// send a request to start TLS handshake
|
||||||
|
sendMessage("STARTTLS");
|
||||||
|
|
||||||
|
// Wait for the server's response
|
||||||
|
waitForResponse();
|
||||||
|
|
||||||
|
// The response code needs to be 220.
|
||||||
|
if (responseCode != 220) {
|
||||||
|
emit smtpError(ServerError);
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
((QSslSocket*) socket)->startClientEncryption();
|
||||||
|
|
||||||
|
if (!((QSslSocket*) socket)->waitForEncrypted(connectionTimeout)) {
|
||||||
|
qDebug() << ((QSslSocket*) socket)->errorString();
|
||||||
|
emit SmtpError(ConnectionTimeoutError);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send ELHO one more time
|
||||||
|
sendMessage("EHLO " + name);
|
||||||
|
|
||||||
|
// Wait for the server's response
|
||||||
|
waitForResponse();
|
||||||
|
|
||||||
|
// The response code needs to be 250.
|
||||||
|
if (responseCode != 250) {
|
||||||
|
emit smtpError(ServerError);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (ResponseTimeoutException)
|
catch (ResponseTimeoutException)
|
||||||
{
|
{
|
||||||
|
@ -50,7 +50,8 @@ public:
|
|||||||
enum ConnectionType
|
enum ConnectionType
|
||||||
{
|
{
|
||||||
TcpConnection,
|
TcpConnection,
|
||||||
SslConnection
|
SslConnection,
|
||||||
|
TlsConnection // STARTTLS
|
||||||
};
|
};
|
||||||
|
|
||||||
/* [0] --- */
|
/* [0] --- */
|
||||||
@ -88,6 +89,12 @@ public:
|
|||||||
SmtpClient::AuthMethod getAuthMethod() const;
|
SmtpClient::AuthMethod getAuthMethod() const;
|
||||||
void setAuthMethod(AuthMethod method);
|
void setAuthMethod(AuthMethod method);
|
||||||
|
|
||||||
|
const QString & getResponseText() const;
|
||||||
|
int getResponseCode() const;
|
||||||
|
|
||||||
|
QTcpSocket* getSocket();
|
||||||
|
|
||||||
|
|
||||||
/* [2] --- */
|
/* [2] --- */
|
||||||
|
|
||||||
|
|
||||||
@ -113,7 +120,7 @@ protected:
|
|||||||
|
|
||||||
QString host;
|
QString host;
|
||||||
int port;
|
int port;
|
||||||
bool useSsl;
|
ConnectionType connectionType;
|
||||||
QString name;
|
QString name;
|
||||||
|
|
||||||
QString user;
|
QString user;
|
||||||
|
Loading…
Reference in New Issue
Block a user