Integrate changes from olehs's repository:

https://github.com/olehs/SmtpClient-for-Qt
This commit is contained in:
Attila Tőkés 2014-11-09 22:18:04 +02:00
parent ace683a658
commit b90aa7cfc3
18 changed files with 196 additions and 141 deletions

View File

@ -13,10 +13,10 @@ int main(int argc, char *argv[])
MimeMessage message; MimeMessage message;
EmailAddress sender("your_email_address@host.com", "Your Name"); EmailAddress sender("your_email_address@host.com", "Your Name");
message.setSender(&sender); message.setSender(sender);
EmailAddress to("recipient@host.com", "Recipient's Name"); EmailAddress to("recipient@host.com", "Recipient's Name");
message.addRecipient(&to); message.addRecipient(to);
message.setSubject("SmtpClient for Qt - Demo"); message.setSubject("SmtpClient for Qt - Demo");

View File

@ -37,7 +37,7 @@ SendEmail::~SendEmail()
delete ui; delete ui;
} }
EmailAddress* SendEmail::stringToEmail(const QString &str) EmailAddress SendEmail::stringToEmail(const QString &str)
{ {
int p1 = str.indexOf("<"); int p1 = str.indexOf("<");
int p2 = str.indexOf(">"); int p2 = str.indexOf(">");
@ -45,11 +45,11 @@ EmailAddress* SendEmail::stringToEmail(const QString &str)
if (p1 == -1) if (p1 == -1)
{ {
// no name, only email address // no name, only email address
return new EmailAddress(str); return EmailAddress(str);
} }
else else
{ {
return new EmailAddress(str.mid(p1 + 1, p2 - p1 - 1), str.left(p1)); return EmailAddress(str.mid(p1 + 1, p2 - p1 - 1), str.left(p1));
} }
} }
@ -59,11 +59,8 @@ void SendEmail::on_addAttachment_clicked()
QFileDialog dialog(this); QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::ExistingFiles); dialog.setFileMode(QFileDialog::ExistingFiles);
if (dialog.exec()) if (dialog.exec())
ui->attachments->addItems(dialog.selectedFiles()); ui->attachments->addItems(dialog.selectedFiles());
} }
void SendEmail::on_sendEmail_clicked() void SendEmail::on_sendEmail_clicked()
@ -75,7 +72,7 @@ void SendEmail::on_sendEmail_clicked()
QString user = ui->username->text(); QString user = ui->username->text();
QString password = ui->password->text(); QString password = ui->password->text();
EmailAddress *sender = stringToEmail(ui->sender->text()); EmailAddress sender = stringToEmail(ui->sender->text());
QStringList rcptStringList = ui->recipients->text().split(';'); QStringList rcptStringList = ui->recipients->text().split(';');

View File

@ -33,7 +33,7 @@ public:
explicit SendEmail(QWidget *parent = 0); explicit SendEmail(QWidget *parent = 0);
~SendEmail(); ~SendEmail();
static EmailAddress * stringToEmail(const QString & str); static EmailAddress stringToEmail(const QString & str);
private slots: private slots:
void on_addAttachment_clicked(); void on_addAttachment_clicked();

View File

@ -26,10 +26,10 @@ int main(int argc, char *argv[])
MimeMessage message; MimeMessage message;
EmailAddress sender("your_email_address@host.com", "Your Name"); EmailAddress sender("your_email_address@host.com", "Your Name");
message.setSender(&sender); message.setSender(sender);
EmailAddress to("recipient@host.com", "Recipient's Name"); EmailAddress to("recipient@host.com", "Recipient's Name");
message.addRecipient(&to); message.addRecipient(to);
message.setSubject("SmtpClient for Qt - Demo"); message.setSubject("SmtpClient for Qt - Demo");

View File

@ -27,10 +27,10 @@ int main(int argc, char *argv[])
MimeMessage message; MimeMessage message;
EmailAddress sender("your_email_address@host.com", "Your Name"); EmailAddress sender("your_email_address@host.com", "Your Name");
message.setSender(&sender); message.setSender(sender);
EmailAddress to("recipient@host.com", "Recipient's Name"); EmailAddress to("recipient@host.com", "Recipient's Name");
message.addRecipient(&to); message.addRecipient(to);
message.setSubject("SmtpClient for Qt - Example 3 - Html email with images"); message.setSubject("SmtpClient for Qt - Example 3 - Html email with images");

View File

@ -21,9 +21,13 @@
/* [1] Constructors and Destructors */ /* [1] Constructors and Destructors */
EmailAddress::EmailAddress(const QString & address, const QString & name) EmailAddress::EmailAddress(const QString & address, const QString & name)
: address(address), name(name)
{
}
EmailAddress::EmailAddress(const EmailAddress &other)
: address(other.address), name(other.name)
{ {
this->address = address;
this->name = name;
} }
EmailAddress::~EmailAddress() EmailAddress::~EmailAddress()
@ -35,23 +39,13 @@ EmailAddress::~EmailAddress()
/* [2] Getters and Setters */ /* [2] Getters and Setters */
void EmailAddress::setName(const QString & name)
{
this->name = name;
} QString EmailAddress::getName() const
void EmailAddress::setAddress(const QString & address)
{
this->address = address;
}
const QString & EmailAddress::getName() const
{ {
return name; return name;
} }
const QString & EmailAddress::getAddress() const QString EmailAddress::getAddress() const
{ {
return address; return address;
} }

View File

@ -28,8 +28,8 @@ public:
/* [1] Constructors and Destructors */ /* [1] Constructors and Destructors */
EmailAddress(); EmailAddress(const QString & address = "", const QString & name = "");
EmailAddress(const QString & address, const QString & name=""); EmailAddress(const EmailAddress &other);
~EmailAddress(); ~EmailAddress();
@ -37,11 +37,9 @@ public:
/* [2] Getters and Setters */ /* [2] Getters and Setters */
void setName(const QString & name);
void setAddress(const QString & address);
const QString & getName() const; QString getAddress() const;
const QString & getAddress() const; QString getName() const;
/* [2] --- */ /* [2] --- */
@ -50,8 +48,8 @@ private:
/* [3] Private members */ /* [3] Private members */
QString name;
QString address; QString address;
QString name;
/* [3] --- */ /* [3] --- */
}; };

View File

@ -4,7 +4,6 @@ MimeBase64Formatter::MimeBase64Formatter(QIODevice *out) :
MimeContentFormatter(out) {} MimeContentFormatter(out) {}
qint64 MimeBase64Formatter::writeData(const char *data, qint64 maxLength) { qint64 MimeBase64Formatter::writeData(const char *data, qint64 maxLength) {
qDebug("called");
int lines = (maxLength - 1) / lineLength + 1; int lines = (maxLength - 1) / lineLength + 1;
for (int i = 1; i < lines; ++i) { for (int i = 1; i < lines; ++i) {
output->write(data, lineLength); output->write(data, lineLength);

View File

@ -37,7 +37,7 @@ void MimeHtml::setHtml(const QString & html)
this->text = html; this->text = html;
} }
const QString & MimeHtml::getHtml() const QString MimeHtml::getHtml() const
{ {
return text; return text;
} }

View File

@ -38,7 +38,7 @@ public:
void setHtml(const QString & html); void setHtml(const QString & html);
const QString& getHtml() const; QString getHtml() const;
/* [2] --- */ /* [2] --- */

View File

@ -23,7 +23,7 @@
MimeInlineFile::MimeInlineFile(QFile *f) MimeInlineFile::MimeInlineFile(QFile *f)
: MimeFile(f) : MimeFile(f)
{ {
this->headerLines += "Content-Disposition: inline\r\n"; addHeaderLine("Content-Disposition: inline");
} }
MimeInlineFile::~MimeInlineFile() MimeInlineFile::~MimeInlineFile()

View File

@ -49,12 +49,12 @@ void MimeMessage::setContent(MimePart *content) {
this->content = content; this->content = content;
} }
void MimeMessage::setSender(EmailAddress* e) void MimeMessage::setSender(const EmailAddress &sender)
{ {
this->sender = e; this->sender = sender;
} }
void MimeMessage::addRecipient(EmailAddress* rcpt, RecipientType type) void MimeMessage::addRecipient(const EmailAddress &rcpt, RecipientType type)
{ {
switch (type) switch (type)
{ {
@ -70,18 +70,23 @@ void MimeMessage::addRecipient(EmailAddress* rcpt, RecipientType type)
} }
} }
void MimeMessage::addTo(EmailAddress* rcpt) { void MimeMessage::addTo(const EmailAddress &rcpt) {
this->recipientsTo << rcpt; this->recipientsTo << rcpt;
} }
void MimeMessage::addCc(EmailAddress* rcpt) { void MimeMessage::addCc(const EmailAddress &rcpt) {
this->recipientsCc << rcpt; this->recipientsCc << rcpt;
} }
void MimeMessage::addBcc(EmailAddress* rcpt) { void MimeMessage::addBcc(const EmailAddress &rcpt) {
this->recipientsBcc << rcpt; this->recipientsBcc << rcpt;
} }
void MimeMessage::addCustomHeader(const QString &header)
{
this->customHeaders << header;
}
void MimeMessage::setSubject(const QString & subject) void MimeMessage::setSubject(const QString & subject)
{ {
this->subject = subject; this->subject = subject;
@ -99,12 +104,12 @@ void MimeMessage::setHeaderEncoding(MimePart::Encoding hEnc)
this->hEncoding = hEnc; this->hEncoding = hEnc;
} }
const EmailAddress & MimeMessage::getSender() const EmailAddress MimeMessage::getSender() const
{ {
return *sender; return sender;
} }
const QList<EmailAddress*> & MimeMessage::getRecipients(RecipientType type) const const QList<EmailAddress> & MimeMessage::getRecipients(RecipientType type) const
{ {
switch (type) switch (type)
{ {
@ -118,7 +123,7 @@ const QList<EmailAddress*> & MimeMessage::getRecipients(RecipientType type) cons
} }
} }
const QString & MimeMessage::getSubject() const QString MimeMessage::getSubject() const
{ {
return subject; return subject;
} }
@ -148,23 +153,30 @@ QString MimeMessage::toString() const
return QString(out.buffer()); return QString(out.buffer());
} }
QByteArray MimeMessage::formatAddress(EmailAddress *address, MimePart::Encoding encoding) { QByteArray MimeMessage::formatAddress(const EmailAddress &address, MimePart::Encoding encoding) {
QByteArray result; QByteArray result;
if (!address->getName().isEmpty()) result.append(format(address.getName(), encoding));
result.append(" <" + address.getAddress() + ">");
return result;
}
QByteArray MimeMessage::format(const QString &text, MimePart::Encoding encoding)
{
QByteArray result;
if (!text.isEmpty())
{ {
switch (encoding) switch (encoding)
{ {
case MimePart::Base64: case MimePart::Base64:
result.append(" =?utf-8?B?" + address->getName().toLocal8Bit().toBase64() + "?="); result.append(" =?utf-8?B?" + text.toUtf8().toBase64() + "?=");
break; break;
case MimePart::QuotedPrintable: case MimePart::QuotedPrintable:
result.append(" =?utf-8?Q?" + QuotedPrintable::encode(address->getName().toLocal8Bit()).toLocal8Bit().replace(' ', "_").replace(':',"=3A") + "?="); result.append(" =?utf-8?Q?" + QuotedPrintable::encode(text.toUtf8()).toLocal8Bit().replace(' ', "_").replace(':',"=3A") + "?=");
break; break;
default: default:
result.append(" ").append(address->getName().toLocal8Bit()); result.append(" ").append(text.toLocal8Bit());
} }
} }
result.append(" <" + address->getAddress() + ">");
return result; return result;
} }
@ -202,22 +214,15 @@ void MimeMessage::writeToDevice(QIODevice &out) const {
/* ------------ Subject ------------- */ /* ------------ Subject ------------- */
header.append("Subject: "); header.append("Subject: ");
header.append(format(subject, hEncoding));
header.append("\r\n");
switch (hEncoding)
{
case MimePart::Base64:
header.append("=?utf-8?B?" + subject.toLocal8Bit().toBase64() + "?=");
break;
case MimePart::QuotedPrintable:
header.append("=?utf-8?Q?" + QuotedPrintable::encode(subject.toLocal8Bit()).toLocal8Bit().replace(' ', "_").replace(':',"=3A") + "?=");
break;
default:
header.append(subject);
}
/* ---------------------------------- */ /* ---------------------------------- */
foreach (QString hdr, customHeaders) {
header.append(hdr.toLocal8Bit());
header.append("\r\n"); header.append("\r\n");
}
header.append("MIME-Version: 1.0\r\n"); header.append("MIME-Version: 1.0\r\n");
out.write(header); out.write(header);

View File

@ -19,7 +19,7 @@
#ifndef MIMEMESSAGE_H #ifndef MIMEMESSAGE_H
#define MIMEMESSAGE_H #define MIMEMESSAGE_H
#include <QList> #include <QStringList>
#include <QTextStream> #include <QTextStream>
#include "smtpmime_global.h" #include "smtpmime_global.h"
@ -39,7 +39,7 @@ public:
/* [1] Constructors and Destructors */ /* [1] Constructors and Destructors */
MimeMessage(bool createAutoMimeConent = true); MimeMessage(bool createAutoMimeContent = true);
~MimeMessage(); ~MimeMessage();
/* [1] --- */ /* [1] --- */
@ -47,19 +47,21 @@ public:
/* [2] Getters and Setters */ /* [2] Getters and Setters */
void setSender(EmailAddress* e); void setSender(const EmailAddress &sndr);
void addRecipient(EmailAddress* rcpt, RecipientType type = To); void addRecipient(const EmailAddress &rcpt, RecipientType type = To);
void addTo(EmailAddress* rcpt); void addTo(const EmailAddress &rcpt);
void addCc(EmailAddress* rcpt); void addCc(const EmailAddress &rcpt);
void addBcc(EmailAddress* rcpt); void addBcc(const EmailAddress &rcpt);
void setSubject(const QString & subject); void addCustomHeader(const QString &hdr);
void setSubject(const QString &subject);
void addPart(MimePart* part); void addPart(MimePart* part);
void setHeaderEncoding(MimePart::Encoding); void setHeaderEncoding(MimePart::Encoding);
const EmailAddress & getSender() const; EmailAddress getSender() const;
const QList<EmailAddress*> & getRecipients(RecipientType type = To) const; const QList<EmailAddress> &getRecipients(RecipientType type = To) const;
const QString & getSubject() const; QString getSubject() const;
const QStringList &getCustomHeaders() const;
const QList<MimePart*> & getParts() const; const QList<MimePart*> & getParts() const;
MimePart& getContent(); MimePart& getContent();
@ -78,14 +80,16 @@ protected:
/* [4] Protected members */ /* [4] Protected members */
EmailAddress* sender; EmailAddress sender;
QList<EmailAddress*> recipientsTo, recipientsCc, recipientsBcc; QList<EmailAddress> recipientsTo, recipientsCc, recipientsBcc;
QString subject; QString subject;
QStringList customHeaders;
MimePart *content; MimePart *content;
MimePart::Encoding hEncoding; MimePart::Encoding hEncoding;
static QByteArray formatAddress(EmailAddress*, MimePart::Encoding); static QByteArray format(const QString &text, MimePart::Encoding encoding);
static QByteArray formatAddress(const EmailAddress &address, MimePart::Encoding encoding);
/* [4] --- */ /* [4] --- */

View File

@ -43,7 +43,9 @@ MimeMultiPart::MimeMultiPart(MultiPartType type)
} }
MimeMultiPart::~MimeMultiPart() { MimeMultiPart::~MimeMultiPart() {
foreach (MimePart *part, parts) {
delete part;
}
} }
void MimeMultiPart::addPart(MimePart *part) { void MimeMultiPart::addPart(MimePart *part) {

View File

@ -58,12 +58,12 @@ void MimePart::addHeaderLine(const QString & line)
this->headerLines += line + "\r\n"; this->headerLines += line + "\r\n";
} }
const QString& MimePart::getHeader() const QString MimePart::getHeader() const
{ {
return headerLines; return headerLines;
} }
const QByteArray& MimePart::getContent() const QByteArray MimePart::getContent() const
{ {
return content; return content;
} }
@ -73,7 +73,7 @@ void MimePart::setContentId(const QString & cId)
this->cId = cId; this->cId = cId;
} }
const QString & MimePart::getContentId() const QString MimePart::getContentId() const
{ {
return this->cId; return this->cId;
} }
@ -83,7 +83,7 @@ void MimePart::setContentName(const QString & cName)
this->cName = cName; this->cName = cName;
} }
const QString & MimePart::getContentName() const QString MimePart::getContentName() const
{ {
return this->cName; return this->cName;
} }
@ -93,7 +93,7 @@ void MimePart::setContentType(const QString & cType)
this->cType = cType; this->cType = cType;
} }
const QString & MimePart::getContentType() const QString MimePart::getContentType() const
{ {
return this->cType; return this->cType;
} }
@ -103,7 +103,7 @@ void MimePart::setCharset(const QString & charset)
this->cCharset = charset; this->cCharset = charset;
} }
const QString & MimePart::getCharset() const QString MimePart::getCharset() const
{ {
return this->cCharset; return this->cCharset;
} }

View File

@ -51,25 +51,25 @@ public:
/* [2] Getters and Setters */ /* [2] Getters and Setters */
const QString& getHeader() const;
const QByteArray& getContent() const;
void setContent(const QByteArray & content); void setContent(const QByteArray & content);
QByteArray getContent() const;
void setHeader(const QString & headerLines); void setHeader(const QString & headerLines);
QString getHeader() const;
void addHeaderLine(const QString & line); void addHeaderLine(const QString & line);
void setContentId(const QString & cId); void setContentId(const QString & cId);
const QString & getContentId() const; QString getContentId() const;
void setContentName(const QString & cName); void setContentName(const QString & cName);
const QString & getContentName() const; QString getContentName() const;
void setContentType(const QString & cType); void setContentType(const QString & cType);
const QString & getContentType() const; QString getContentType() const;
void setCharset(const QString & charset); void setCharset(const QString & charset);
const QString & getCharset() const; QString getCharset() const;
void setEncoding(Encoding enc); void setEncoding(Encoding enc);
Encoding getEncoding() const; Encoding getEncoding() const;

View File

@ -33,7 +33,8 @@ SmtpClient::SmtpClient(const QString & host, int port, ConnectionType connection
name("localhost"), name("localhost"),
isReadyConnected(false), isReadyConnected(false),
isAuthenticated(false), isAuthenticated(false),
isMailSent(false) isMailSent(false),
isReset(false)
{ {
setConnectionType(connectionType); setConnectionType(connectionType);
@ -54,7 +55,7 @@ SmtpClient::~SmtpClient() {}
/** /**
* @brief Returns the host name of the server. * @brief Returns the host name of the server.
*/ */
const QString& SmtpClient::getHost() const QString SmtpClient::getHost() const
{ {
return this->host; return this->host;
} }
@ -78,7 +79,7 @@ SmtpClient::ConnectionType SmtpClient::getConnectionType() const
/** /**
* @brief Returns the client's name. * @brief Returns the client's name.
*/ */
const QString& SmtpClient::getName() const QString SmtpClient::getName() const
{ {
return this->name; return this->name;
} }
@ -94,7 +95,7 @@ void SmtpClient::setName(const QString &name)
/** /**
* @brief Returns the last response of the server. * @brief Returns the last response of the server.
*/ */
const QString & SmtpClient::getResponseText() const QString SmtpClient::getResponseText() const
{ {
return responseText; return responseText;
} }
@ -160,19 +161,24 @@ void SmtpClient::quit()
changeState(DisconnectingState); changeState(DisconnectingState);
} }
void SmtpClient::reset()
{
if (!isReadyConnected)
return;
isReset = false;
changeState(ResetState);
}
bool SmtpClient::waitForReadyConnected(int msec) { bool SmtpClient::waitForReadyConnected(int msec) {
if (state == UnconnectedState) if (state == UnconnectedState)
return false; return false;
QEventLoop loop;
QObject::connect(this, SIGNAL(readyConnected()), &loop, SLOT(quit()));
if (isReadyConnected) if (isReadyConnected)
return true; return true;
QTimer::singleShot(msec, &loop, SLOT(quit())); waitForEvent(msec, SIGNAL(readyConnected()));
loop.exec();
return isReadyConnected; return isReadyConnected;
} }
@ -181,14 +187,10 @@ bool SmtpClient::waitForAuthenticated(int msec) {
if (!isReadyConnected) if (!isReadyConnected)
return false; return false;
QEventLoop loop;
QObject::connect(this, SIGNAL(authenticated()), &loop, SLOT(quit()));
if (isAuthenticated) if (isAuthenticated)
return true; return true;
QTimer::singleShot(msec, &loop, SLOT(quit())); waitForEvent(msec, SIGNAL(authenticated()));
loop.exec();
return isAuthenticated; return isAuthenticated;
} }
@ -197,18 +199,27 @@ bool SmtpClient::waitForMailSent(int msec) {
if (!isReadyConnected) if (!isReadyConnected)
return false; return false;
QEventLoop loop;
QObject::connect(this, SIGNAL(mailSent()), &loop, SLOT(quit()));
if (isMailSent) if (isMailSent)
return true; return true;
QTimer::singleShot(msec, &loop, SLOT(quit())); waitForEvent(msec, SIGNAL(mailSent()));
loop.exec();
return isMailSent; return isMailSent;
} }
bool SmtpClient::waitForReset(int msec)
{
if (!isReadyConnected)
return false;
if (isReset)
return true;
waitForEvent(msec, SIGNAL(mailReset()));
return isReset;
}
/* [3] --- */ /* [3] --- */
@ -276,8 +287,12 @@ void SmtpClient::changeState(SmtpClient::ClientState state) {
socket->disconnectFromHost(); socket->disconnectFromHost();
break; break;
case ResetState:
sendMessage("RSET");
break;
case _EHLO_State: case _EHLO_State:
// Service ready. Send EHLO message and chage the state // Service ready. Send EHLO message and change the state
sendMessage("EHLO " + name); sendMessage("EHLO " + name);
break; break;
@ -345,7 +360,7 @@ void SmtpClient::changeState(SmtpClient::ClientState state) {
case _MAIL_1_RCPT_INIT: case _MAIL_1_RCPT_INIT:
rcptType++; rcptType++;
const QList<EmailAddress*> *addressList; const QList<EmailAddress> *addressList;
switch (rcptType) switch (rcptType)
{ {
case _TO: case _TO:
@ -368,7 +383,7 @@ void SmtpClient::changeState(SmtpClient::ClientState state) {
case _MAIL_2_RCPT: case _MAIL_2_RCPT:
if (addressIt != addressItEnd) { if (addressIt != addressItEnd) {
sendMessage("RCPT TO: <" + (*addressIt)->getAddress() + ">"); sendMessage("RCPT TO: <" + addressIt->getAddress() + ">");
addressIt++; addressIt++;
} else { } else {
changeState(_MAIL_1_RCPT_INIT); changeState(_MAIL_1_RCPT_INIT);
@ -407,15 +422,26 @@ 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 error(ServerError); return; emitError(ServerError);
return;
} }
changeState(_EHLO_State); changeState(_EHLO_State);
break; break;
case ResetState:
if (responseCode != 250) {
emitError(ServerError);
return;
}
emit mailReset();
changeState(ReadyState);
break;
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 error(ServerError); return; emitError(ServerError);
return;
} }
changeState((connectionType != TlsConnection) ? _READY_Connected : _TLS_State); changeState((connectionType != TlsConnection) ? _READY_Connected : _TLS_State);
@ -425,7 +451,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 error(ServerError); emitError(ServerError);
return; return;
} }
changeState(_TLS_1_ENCRYPT); changeState(_TLS_1_ENCRYPT);
@ -434,7 +460,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 error(ServerError); emitError(ServerError);
return; return;
} }
changeState(_READY_Encrypted); changeState(_READY_Encrypted);
@ -444,7 +470,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 error(AuthenticationError); emitError(AuthenticationError);
return; return;
} }
changeState(_READY_Authenticated); changeState(_READY_Authenticated);
@ -452,7 +478,7 @@ void SmtpClient::processResponse() {
case _AUTH_LOGIN_0: case _AUTH_LOGIN_0:
if (responseCode != 334) { if (responseCode != 334) {
emit error(AuthenticationError); emitError(AuthenticationError);
return; return;
} }
changeState(_AUTH_LOGIN_1_USER); changeState(_AUTH_LOGIN_1_USER);
@ -460,7 +486,7 @@ void SmtpClient::processResponse() {
case _AUTH_LOGIN_1_USER: case _AUTH_LOGIN_1_USER:
if (responseCode != 334) { if (responseCode != 334) {
emit error(AuthenticationError); emitError(AuthenticationError);
return; return;
} }
changeState(_AUTH_LOGIN_2_PASS); changeState(_AUTH_LOGIN_2_PASS);
@ -468,7 +494,7 @@ void SmtpClient::processResponse() {
case _AUTH_LOGIN_2_PASS: case _AUTH_LOGIN_2_PASS:
if (responseCode != 235) { if (responseCode != 235) {
emit error(AuthenticationError); emitError(AuthenticationError);
return; return;
} }
changeState(_READY_Authenticated); changeState(_READY_Authenticated);
@ -477,7 +503,7 @@ void SmtpClient::processResponse() {
/* --- MAIL --- */ /* --- MAIL --- */
case _MAIL_0_FROM: case _MAIL_0_FROM:
if (responseCode != 250) { if (responseCode != 250) {
emit error(MailSendingError); emitError(MailSendingError);
return; return;
} }
changeState(_MAIL_1_RCPT_INIT); changeState(_MAIL_1_RCPT_INIT);
@ -485,7 +511,7 @@ void SmtpClient::processResponse() {
case _MAIL_2_RCPT: case _MAIL_2_RCPT:
if (responseCode != 250) { if (responseCode != 250) {
emit error(MailSendingError); emitError(MailSendingError);
return; return;
} }
changeState(_MAIL_2_RCPT); changeState(_MAIL_2_RCPT);
@ -493,7 +519,7 @@ void SmtpClient::processResponse() {
case _MAIL_3_DATA: case _MAIL_3_DATA:
if (responseCode != 354) { if (responseCode != 354) {
emit error(MailSendingError); emitError(MailSendingError);
return; return;
} }
changeState(_MAIL_4_SEND_DATA); changeState(_MAIL_4_SEND_DATA);
@ -501,7 +527,7 @@ void SmtpClient::processResponse() {
case _MAIL_4_SEND_DATA: case _MAIL_4_SEND_DATA:
if (responseCode != 250) { if (responseCode != 250) {
emit error(MailSendingError); emitError(MailSendingError);
return; return;
} }
changeState(_READY_MailSent); changeState(_READY_MailSent);
@ -523,6 +549,28 @@ void SmtpClient::sendMessage(const QString &text)
socket->write(text.toUtf8() + "\r\n"); socket->write(text.toUtf8() + "\r\n");
} }
void SmtpClient::emitError(SmtpClient::SmtpError e)
{
emit error(e);
}
void SmtpClient::waitForEvent(int msec, const char *successSignal)
{
QEventLoop loop;
QObject::connect(this, successSignal, &loop, SLOT(quit()));
QObject::connect(this, SIGNAL(error(SmtpClient::SmtpError)), &loop, SLOT(quit()));
if(msec > 0)
{
QTimer timer;
timer.setSingleShot(true);
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
timer.start(msec);
}
loop.exec();
}
/* [4] --- */ /* [4] --- */
@ -551,8 +599,6 @@ void SmtpClient::socketStateChanged(QAbstractSocket::SocketState state) {
void SmtpClient::socketError(QAbstractSocket::SocketError socketError) { void SmtpClient::socketError(QAbstractSocket::SocketError socketError) {
#ifndef QT_NO_DEBUG #ifndef QT_NO_DEBUG
qDebug() << "[Socket] ERROR:" << socketError; qDebug() << "[Socket] ERROR:" << socketError;
#else
Q_UNUSED(socketError);
#endif #endif
emit error(SocketError); emit error(SocketError);
} }
@ -583,13 +629,13 @@ void SmtpClient::socketReadyRead()
// Check for server error // Check for server error
if (responseCode / 100 == 4) { if (responseCode / 100 == 4) {
emit error(ServerError); emitError(ServerError);
return; return;
} }
// Check for client error // Check for client error
if (responseCode / 100 == 5) { if (responseCode / 100 == 5) {
emit error(ClientError); emitError(ClientError);
return; return;
} }

View File

@ -66,6 +66,7 @@ public:
AuthenticatingState = 4, AuthenticatingState = 4,
MailSendingState = 5, MailSendingState = 5,
DisconnectingState = 6, DisconnectingState = 6,
ResetState = 7,
/* Internal States */ /* Internal States */
_EHLO_State = 50, _EHLO_State = 50,
@ -102,7 +103,7 @@ public:
/* [1] Constructors and Destructors */ /* [1] Constructors and Destructors */
SmtpClient(const QString & host = "locahost", int port = 25, ConnectionType ct = TcpConnection); SmtpClient(const QString & host = "localhost", int port = 25, ConnectionType ct = TcpConnection);
~SmtpClient(); ~SmtpClient();
@ -111,14 +112,14 @@ public:
/* [2] Getters and Setters */ /* [2] Getters and Setters */
const QString& getHost() const; QString getHost() const;
int getPort() const; int getPort() const;
ConnectionType getConnectionType() const; ConnectionType getConnectionType() const;
const QString& getName() const; QString getName() const;
void setName(const QString &name); void setName(const QString &name);
const QString & getResponseText() const; QString getResponseText() const;
int getResponseCode() const; int getResponseCode() const;
QTcpSocket* getSocket(); QTcpSocket* getSocket();
@ -132,10 +133,15 @@ public:
void login(const QString &user, const QString &password, AuthMethod method = AuthLogin); void login(const QString &user, const QString &password, AuthMethod method = AuthLogin);
void sendMail(const MimeMessage & email); void sendMail(const MimeMessage & email);
void quit(); void quit();
void reset();
bool isConnected();
bool isLogged();
bool waitForReadyConnected(int msec = 30000); bool waitForReadyConnected(int msec = 30000);
bool waitForAuthenticated(int msec = 30000); bool waitForAuthenticated(int msec = 30000);
bool waitForMailSent(int msec = 30000); bool waitForMailSent(int msec = 30000);
bool waitForReset(int msec = 30000);
/* [3] --- */ /* [3] --- */
@ -170,14 +176,15 @@ protected:
bool isReadyConnected; bool isReadyConnected;
bool isAuthenticated; bool isAuthenticated;
bool isMailSent; bool isMailSent;
bool isReset;
const MimeMessage *email; const MimeMessage *email;
int rcptType; int rcptType;
enum _RcptType { _TO = 1, _CC = 2, _BCC = 3}; enum _RcptType { _TO = 1, _CC = 2, _BCC = 3};
QList<EmailAddress*>::const_iterator addressIt; QList<EmailAddress>::const_iterator addressIt;
QList<EmailAddress*>::const_iterator addressItEnd; QList<EmailAddress>::const_iterator addressItEnd;
/* [4] --- */ /* [4] --- */
@ -188,6 +195,8 @@ protected:
void changeState(ClientState state); void changeState(ClientState state);
void processResponse(); void processResponse();
void sendMessage(const QString &text); void sendMessage(const QString &text);
void emitError(SmtpClient::SmtpError e);
void waitForEvent(int msec, const char *successSignal);
/* [5] --- */ /* [5] --- */
@ -213,6 +222,7 @@ signals:
void readyConnected(); void readyConnected();
void authenticated(); void authenticated();
void mailSent(); void mailSent();
void mailReset();
void disconnected(); void disconnected();
/* [7] --- */ /* [7] --- */