Added support for synchronous mode: waitForReadyConnected(),
waitForAuthenticated(), waitForMailSent(). The waitForResponse() funtion was deleted (no more needed).
This commit is contained in:
parent
297f2f8178
commit
afb66b4fff
@ -20,6 +20,8 @@
|
|||||||
|
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
|
#include <QTimer>
|
||||||
|
#include <QEventLoop>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -31,7 +33,8 @@ SmtpClient::SmtpClient(const QString & host, int port, ConnectionType connection
|
|||||||
name("localhost"),
|
name("localhost"),
|
||||||
authMethod(AuthPlain),
|
authMethod(AuthPlain),
|
||||||
connectionTimeout(5000),
|
connectionTimeout(5000),
|
||||||
responseTimeout(5000)
|
responseTimeout(5000),
|
||||||
|
isReadyConnected(false)
|
||||||
{
|
{
|
||||||
setConnectionType(connectionType);
|
setConnectionType(connectionType);
|
||||||
|
|
||||||
@ -189,20 +192,41 @@ void SmtpClient::quit()
|
|||||||
changeState(DisconnectingState);
|
changeState(DisconnectingState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool SmtpClient::waitForReadyConnected(int msec) {
|
||||||
|
QEventLoop loop;
|
||||||
|
|
||||||
|
QTimer::singleShot(msec, &loop, SLOT(quit()));
|
||||||
|
QObject::connect(this, SIGNAL(readyConnected()), &loop, SLOT(quit()));
|
||||||
|
|
||||||
|
loop.exec();
|
||||||
|
return isReadyConnected;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SmtpClient::waitForAuthenticated(int msec) {
|
||||||
|
QEventLoop loop;
|
||||||
|
|
||||||
|
QTimer::singleShot(msec, &loop, SLOT(quit()));
|
||||||
|
QObject::connect(this, SIGNAL(authenticated()), &loop, SLOT(quit()));
|
||||||
|
|
||||||
|
loop.exec();
|
||||||
|
return isAuthenticated;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SmtpClient::waitForMailSent(int msec) {
|
||||||
|
QEventLoop loop;
|
||||||
|
|
||||||
|
QTimer::singleShot(msec, &loop, SLOT(quit()));
|
||||||
|
QObject::connect(this, SIGNAL(mailSent()), &loop, SLOT(quit()));
|
||||||
|
|
||||||
|
loop.exec();
|
||||||
|
return isMailSent;
|
||||||
|
}
|
||||||
|
|
||||||
/* [3] --- */
|
/* [3] --- */
|
||||||
|
|
||||||
|
|
||||||
/* [4] Protected methods */
|
/* [4] Protected methods */
|
||||||
void SmtpClient::waitForResponse() throw (ResponseTimeoutException)
|
|
||||||
{
|
|
||||||
if (!socket->waitForReadyRead(responseTimeout))
|
|
||||||
{
|
|
||||||
emit smtpError(ResponseTimeoutError);
|
|
||||||
throw ResponseTimeoutException();
|
|
||||||
}
|
|
||||||
|
|
||||||
processResponse();
|
|
||||||
}
|
|
||||||
|
|
||||||
void SmtpClient::changeState(ClientState state) {
|
void SmtpClient::changeState(ClientState state) {
|
||||||
this->state = state;
|
this->state = state;
|
||||||
@ -234,10 +258,12 @@ void SmtpClient::changeState(ClientState state) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case AuthenticatingState:
|
case AuthenticatingState:
|
||||||
|
isAuthenticated = false;
|
||||||
changeState(authMethod == AuthPlain ? _AUTH_PLAIN_0 : _AUTH_LOGIN_0);
|
changeState(authMethod == AuthPlain ? _AUTH_PLAIN_0 : _AUTH_LOGIN_0);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MailSendingState:
|
case MailSendingState:
|
||||||
|
isMailSent = false;
|
||||||
changeState(_MAIL_0_FROM);
|
changeState(_MAIL_0_FROM);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -252,6 +278,7 @@ void SmtpClient::changeState(ClientState state) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case _READY_Connected:
|
case _READY_Connected:
|
||||||
|
isReadyConnected = true;
|
||||||
changeState(ReadyState);
|
changeState(ReadyState);
|
||||||
emit readyConnected();
|
emit readyConnected();
|
||||||
break;
|
break;
|
||||||
@ -301,6 +328,7 @@ void SmtpClient::changeState(ClientState state) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case _READY_Authenticated:
|
case _READY_Authenticated:
|
||||||
|
isAuthenticated = true;
|
||||||
changeState(ReadyState);
|
changeState(ReadyState);
|
||||||
emit authenticated();
|
emit authenticated();
|
||||||
break;
|
break;
|
||||||
@ -349,9 +377,10 @@ void SmtpClient::changeState(ClientState state) {
|
|||||||
sendMessage(".");
|
sendMessage(".");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _READY_MailSended:
|
case _READY_MailSent:
|
||||||
|
isMailSent = true;
|
||||||
changeState(ReadyState);
|
changeState(ReadyState);
|
||||||
emit mailSended();
|
emit mailSent();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -366,7 +395,7 @@ void SmtpClient::processResponse() {
|
|||||||
case ConnectedState:
|
case ConnectedState:
|
||||||
// Just connected to the server. Wait for 220 (Service ready)
|
// Just connected to the server. Wait for 220 (Service ready)
|
||||||
if (responseCode != 220) {
|
if (responseCode != 220) {
|
||||||
emit smtpError(ServerError); return;
|
emit error(ServerError); return;
|
||||||
}
|
}
|
||||||
changeState(_EHLO_State);
|
changeState(_EHLO_State);
|
||||||
break;
|
break;
|
||||||
@ -374,7 +403,7 @@ void SmtpClient::processResponse() {
|
|||||||
case _EHLO_State:
|
case _EHLO_State:
|
||||||
// The response code needs to be 250.
|
// The response code needs to be 250.
|
||||||
if (responseCode != 250) {
|
if (responseCode != 250) {
|
||||||
emit smtpError(ServerError); return;
|
emit error(ServerError); return;
|
||||||
}
|
}
|
||||||
|
|
||||||
changeState((connectionType != TlsConnection) ? _READY_Connected : _TLS_State);
|
changeState((connectionType != TlsConnection) ? _READY_Connected : _TLS_State);
|
||||||
@ -384,7 +413,7 @@ void SmtpClient::processResponse() {
|
|||||||
case _TLS_0_STARTTLS:
|
case _TLS_0_STARTTLS:
|
||||||
// The response code needs to be 220.
|
// The response code needs to be 220.
|
||||||
if (responseCode != 220) {
|
if (responseCode != 220) {
|
||||||
emit smtpError(ServerError);
|
emit error(ServerError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
changeState(_TLS_1_ENCRYPT);
|
changeState(_TLS_1_ENCRYPT);
|
||||||
@ -393,7 +422,7 @@ void SmtpClient::processResponse() {
|
|||||||
case _TLS_2_EHLO:
|
case _TLS_2_EHLO:
|
||||||
// The response code needs to be 250.
|
// The response code needs to be 250.
|
||||||
if (responseCode != 250) {
|
if (responseCode != 250) {
|
||||||
emit smtpError(ServerError);
|
emit error(ServerError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
changeState(_READY_Encrypted);
|
changeState(_READY_Encrypted);
|
||||||
@ -403,7 +432,7 @@ void SmtpClient::processResponse() {
|
|||||||
case _AUTH_PLAIN_0:
|
case _AUTH_PLAIN_0:
|
||||||
// If the response is not 235 then the authentication was failed
|
// If the response is not 235 then the authentication was failed
|
||||||
if (responseCode != 235) {
|
if (responseCode != 235) {
|
||||||
emit smtpError(AuthenticationError);
|
emit error(AuthenticationError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
changeState(_READY_Authenticated);
|
changeState(_READY_Authenticated);
|
||||||
@ -411,7 +440,7 @@ void SmtpClient::processResponse() {
|
|||||||
|
|
||||||
case _AUTH_LOGIN_0:
|
case _AUTH_LOGIN_0:
|
||||||
if (responseCode != 334) {
|
if (responseCode != 334) {
|
||||||
emit smtpError(AuthenticationError);
|
emit error(AuthenticationError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
changeState(_AUTH_LOGIN_1_USER);
|
changeState(_AUTH_LOGIN_1_USER);
|
||||||
@ -419,7 +448,7 @@ void SmtpClient::processResponse() {
|
|||||||
|
|
||||||
case _AUTH_LOGIN_1_USER:
|
case _AUTH_LOGIN_1_USER:
|
||||||
if (responseCode != 334) {
|
if (responseCode != 334) {
|
||||||
emit smtpError(AuthenticationError);
|
emit error(AuthenticationError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
changeState(_AUTH_LOGIN_2_PASS);
|
changeState(_AUTH_LOGIN_2_PASS);
|
||||||
@ -427,7 +456,7 @@ void SmtpClient::processResponse() {
|
|||||||
|
|
||||||
case _AUTH_LOGIN_2_PASS:
|
case _AUTH_LOGIN_2_PASS:
|
||||||
if (responseCode != 235) {
|
if (responseCode != 235) {
|
||||||
emit smtpError(AuthenticationError);
|
emit error(AuthenticationError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
changeState(_READY_Authenticated);
|
changeState(_READY_Authenticated);
|
||||||
@ -436,7 +465,7 @@ void SmtpClient::processResponse() {
|
|||||||
/* --- MAIL --- */
|
/* --- MAIL --- */
|
||||||
case _MAIL_0_FROM:
|
case _MAIL_0_FROM:
|
||||||
if (responseCode != 250) {
|
if (responseCode != 250) {
|
||||||
emit smtpError(MailSendingError);
|
emit error(MailSendingError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
changeState(_MAIL_1_RCPT_INIT);
|
changeState(_MAIL_1_RCPT_INIT);
|
||||||
@ -444,7 +473,7 @@ void SmtpClient::processResponse() {
|
|||||||
|
|
||||||
case _MAIL_2_RCPT:
|
case _MAIL_2_RCPT:
|
||||||
if (responseCode != 250) {
|
if (responseCode != 250) {
|
||||||
emit smtpError(MailSendingError);
|
emit error(MailSendingError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
changeState(_MAIL_2_RCPT);
|
changeState(_MAIL_2_RCPT);
|
||||||
@ -452,7 +481,7 @@ void SmtpClient::processResponse() {
|
|||||||
|
|
||||||
case _MAIL_3_DATA:
|
case _MAIL_3_DATA:
|
||||||
if (responseCode != 354) {
|
if (responseCode != 354) {
|
||||||
emit smtpError(MailSendingError);
|
emit error(MailSendingError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
changeState(_MAIL_4_SEND_DATA);
|
changeState(_MAIL_4_SEND_DATA);
|
||||||
@ -460,10 +489,10 @@ void SmtpClient::processResponse() {
|
|||||||
|
|
||||||
case _MAIL_4_SEND_DATA:
|
case _MAIL_4_SEND_DATA:
|
||||||
if (responseCode != 250) {
|
if (responseCode != 250) {
|
||||||
emit smtpError(MailSendingError);
|
emit error(MailSendingError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
changeState(_READY_MailSended);
|
changeState(_READY_MailSent);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
@ -499,8 +528,7 @@ void SmtpClient::socketStateChanged(QAbstractSocket::SocketState state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SmtpClient::socketError(QAbstractSocket::SocketError socketError) {
|
void SmtpClient::socketError(QAbstractSocket::SocketError socketError) {
|
||||||
qDebug() << "this is sparta" << endl;
|
emit error(SocketError);
|
||||||
emit smtpError(SocketError);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SmtpClient::socketReadyRead()
|
void SmtpClient::socketReadyRead()
|
||||||
@ -517,13 +545,13 @@ void SmtpClient::socketReadyRead()
|
|||||||
|
|
||||||
// Check for server error
|
// Check for server error
|
||||||
if (responseCode / 100 == 4) {
|
if (responseCode / 100 == 4) {
|
||||||
emit smtpError(ServerError);
|
emit error(ServerError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for client error
|
// Check for client error
|
||||||
if (responseCode / 100 == 5) {
|
if (responseCode / 100 == 5) {
|
||||||
emit smtpError(ClientError);
|
emit error(ClientError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QtNetwork/QSslSocket>
|
#include <QtNetwork/QSslSocket>
|
||||||
|
#include <QEventLoop>
|
||||||
|
|
||||||
#include "mimemessage.h"
|
#include "mimemessage.h"
|
||||||
|
|
||||||
@ -71,7 +72,7 @@ public:
|
|||||||
|
|
||||||
_READY_Connected = 52,
|
_READY_Connected = 52,
|
||||||
_READY_Authenticated = 53,
|
_READY_Authenticated = 53,
|
||||||
_READY_MailSended = 54,
|
_READY_MailSent = 54,
|
||||||
_READY_Encrypted = 55,
|
_READY_Encrypted = 55,
|
||||||
|
|
||||||
/* Internal Substates */
|
/* Internal Substates */
|
||||||
@ -142,15 +143,15 @@ public:
|
|||||||
/* [3] Public methods */
|
/* [3] Public methods */
|
||||||
|
|
||||||
bool connectToHost();
|
bool connectToHost();
|
||||||
|
|
||||||
bool login();
|
bool login();
|
||||||
bool login(const QString &user, const QString &password,
|
bool login(const QString &user, const QString &password,
|
||||||
AuthMethod method = AuthLogin);
|
AuthMethod method = AuthLogin);
|
||||||
|
|
||||||
bool sendMail(MimeMessage& email);
|
bool sendMail(MimeMessage& email);
|
||||||
|
|
||||||
void quit();
|
void quit();
|
||||||
|
|
||||||
|
bool waitForReadyConnected(int msec = 30000);
|
||||||
|
bool waitForAuthenticated(int msec = 30000);
|
||||||
|
bool waitForMailSent(int msec = 30000);
|
||||||
|
|
||||||
/* [3] --- */
|
/* [3] --- */
|
||||||
|
|
||||||
@ -178,6 +179,10 @@ protected:
|
|||||||
QString tempResponse;
|
QString tempResponse;
|
||||||
int responseCode;
|
int responseCode;
|
||||||
|
|
||||||
|
bool isReadyConnected;
|
||||||
|
bool isAuthenticated;
|
||||||
|
bool isMailSent;
|
||||||
|
|
||||||
MimeMessage *email;
|
MimeMessage *email;
|
||||||
QList<EmailAddress*>::const_iterator addressIt;
|
QList<EmailAddress*>::const_iterator addressIt;
|
||||||
const QList<EmailAddress*> *addressList;
|
const QList<EmailAddress*> *addressList;
|
||||||
@ -185,8 +190,6 @@ protected:
|
|||||||
int rcptType;
|
int rcptType;
|
||||||
enum _RcptType { _TO = 1, _CC = 2, _BCC = 3};
|
enum _RcptType { _TO = 1, _CC = 2, _BCC = 3};
|
||||||
|
|
||||||
class ResponseTimeoutException {};
|
|
||||||
|
|
||||||
/* [4] --- */
|
/* [4] --- */
|
||||||
|
|
||||||
|
|
||||||
@ -195,8 +198,6 @@ protected:
|
|||||||
|
|
||||||
void processResponse();
|
void processResponse();
|
||||||
|
|
||||||
void waitForResponse() throw (ResponseTimeoutException);
|
|
||||||
|
|
||||||
void sendMessage(const QString &text);
|
void sendMessage(const QString &text);
|
||||||
|
|
||||||
/* [5] --- */
|
/* [5] --- */
|
||||||
@ -217,12 +218,12 @@ signals:
|
|||||||
|
|
||||||
/* [7] Signals */
|
/* [7] Signals */
|
||||||
|
|
||||||
void smtpError(SmtpClient::SmtpError e);
|
void error(SmtpClient::SmtpError e);
|
||||||
void stateChanged(SmtpClient::ClientState s);
|
void stateChanged(SmtpClient::ClientState s);
|
||||||
void connected();
|
void connected();
|
||||||
void readyConnected();
|
void readyConnected();
|
||||||
void authenticated();
|
void authenticated();
|
||||||
void mailSended();
|
void mailSent();
|
||||||
void disconnected();
|
void disconnected();
|
||||||
|
|
||||||
/* [7] --- */
|
/* [7] --- */
|
||||||
|
Loading…
Reference in New Issue
Block a user