- Added support for SendMessage Timeout (Doesn't rely on Reply Timeout)

- Solved small bug in emitting an error message
This commit is contained in:
LascauxSRL 2014-02-20 11:41:21 +01:00
parent 93873f8b1c
commit 4f028793e1
2 changed files with 40 additions and 5 deletions

View File

@ -28,7 +28,8 @@ SmtpClient::SmtpClient(const QString & host, int port, ConnectionType connection
name("localhost"),
authMethod(AuthPlain),
connectionTimeout(5000),
responseTimeout(5000)
responseTimeout(5000),
sendMessageTimeout(60000)
{
setConnectionType(connectionType);
@ -163,6 +164,14 @@ void SmtpClient::setResponseTimeout(int msec)
{
responseTimeout = msec;
}
int SmtpClient::getSendMessageTimeout() const
{
return sendMessageTimeout;
}
void SmtpClient::setSendMessageTimeout(int msec)
{
sendMessageTimeout = msec;
}
/* [2] --- */
@ -233,7 +242,7 @@ bool SmtpClient::connectToHost()
if (!((QSslSocket*) socket)->waitForEncrypted(connectionTimeout)) {
qDebug() << ((QSslSocket*) socket)->errorString();
emit SmtpError(ConnectionTimeoutError);
emit smtpError(ConnectionTimeoutError);
return false;
}
@ -254,6 +263,10 @@ bool SmtpClient::connectToHost()
{
return false;
}
catch (SendMessageTimeoutException)
{
return false;
}
// If no errors occured the function returns true.
return true;
@ -318,6 +331,12 @@ bool SmtpClient::login(const QString &user, const QString &password, AuthMethod
emit smtpError(AuthenticationFailedError);
return false;
}
catch (SendMessageTimeoutException)
{
// Send Timeout exceeded
emit smtpError(AuthenticationFailedError);
return false;
}
return true;
}
@ -384,6 +403,10 @@ bool SmtpClient::sendMail(MimeMessage& email)
{
return false;
}
catch (SendMessageTimeoutException)
{
return false;
}
return true;
}
@ -425,9 +448,14 @@ void SmtpClient::waitForResponse() throw (ResponseTimeoutException)
} while (true);
}
void SmtpClient::sendMessage(const QString &text)
void SmtpClient::sendMessage(const QString &text) throw (SendMessageTimeoutException)
{
socket->write(text.toUtf8() + "\r\n");
if (! socket->waitForBytesWritten(sendMessageTimeout))
{
emit smtpError(SendDataTimeoutError);
throw SendMessageTimeoutException();
}
}
/* [4] --- */

View File

@ -42,6 +42,7 @@ public:
{
ConnectionTimeoutError,
ResponseTimeoutError,
SendDataTimeoutError,
AuthenticationFailedError,
ServerError, // 4xx smtp error
ClientError // 5xx smtp error
@ -98,6 +99,9 @@ public:
int getResponseTimeout() const;
void setResponseTimeout(int msec);
int getSendMessageTimeout() const;
void setSendMessageTimeout(int msec);
QTcpSocket* getSocket();
@ -135,12 +139,15 @@ protected:
int connectionTimeout;
int responseTimeout;
int sendMessageTimeout;
QString responseText;
int responseCode;
class ResponseTimeoutException {};
class SendMessageTimeoutException {};
/* [4] --- */
@ -149,7 +156,7 @@ protected:
void waitForResponse() throw (ResponseTimeoutException);
void sendMessage(const QString &text);
void sendMessage(const QString &text) throw (SendMessageTimeoutException);
/* [5] --- */