Integrate changes from olehs's repository:
https://github.com/olehs/SmtpClient-for-Qt
This commit is contained in:
parent
ace683a658
commit
b90aa7cfc3
@ -13,10 +13,10 @@ int main(int argc, char *argv[])
|
||||
MimeMessage message;
|
||||
|
||||
EmailAddress sender("your_email_address@host.com", "Your Name");
|
||||
message.setSender(&sender);
|
||||
message.setSender(sender);
|
||||
|
||||
EmailAddress to("recipient@host.com", "Recipient's Name");
|
||||
message.addRecipient(&to);
|
||||
message.addRecipient(to);
|
||||
|
||||
message.setSubject("SmtpClient for Qt - Demo");
|
||||
|
||||
|
@ -37,7 +37,7 @@ SendEmail::~SendEmail()
|
||||
delete ui;
|
||||
}
|
||||
|
||||
EmailAddress* SendEmail::stringToEmail(const QString &str)
|
||||
EmailAddress SendEmail::stringToEmail(const QString &str)
|
||||
{
|
||||
int p1 = str.indexOf("<");
|
||||
int p2 = str.indexOf(">");
|
||||
@ -45,11 +45,11 @@ EmailAddress* SendEmail::stringToEmail(const QString &str)
|
||||
if (p1 == -1)
|
||||
{
|
||||
// no name, only email address
|
||||
return new EmailAddress(str);
|
||||
return EmailAddress(str);
|
||||
}
|
||||
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);
|
||||
dialog.setFileMode(QFileDialog::ExistingFiles);
|
||||
|
||||
|
||||
if (dialog.exec())
|
||||
ui->attachments->addItems(dialog.selectedFiles());
|
||||
|
||||
|
||||
}
|
||||
|
||||
void SendEmail::on_sendEmail_clicked()
|
||||
@ -75,7 +72,7 @@ void SendEmail::on_sendEmail_clicked()
|
||||
QString user = ui->username->text();
|
||||
QString password = ui->password->text();
|
||||
|
||||
EmailAddress *sender = stringToEmail(ui->sender->text());
|
||||
EmailAddress sender = stringToEmail(ui->sender->text());
|
||||
|
||||
QStringList rcptStringList = ui->recipients->text().split(';');
|
||||
|
||||
|
@ -33,7 +33,7 @@ public:
|
||||
explicit SendEmail(QWidget *parent = 0);
|
||||
~SendEmail();
|
||||
|
||||
static EmailAddress * stringToEmail(const QString & str);
|
||||
static EmailAddress stringToEmail(const QString & str);
|
||||
|
||||
private slots:
|
||||
void on_addAttachment_clicked();
|
||||
|
@ -26,10 +26,10 @@ int main(int argc, char *argv[])
|
||||
MimeMessage message;
|
||||
|
||||
EmailAddress sender("your_email_address@host.com", "Your Name");
|
||||
message.setSender(&sender);
|
||||
message.setSender(sender);
|
||||
|
||||
EmailAddress to("recipient@host.com", "Recipient's Name");
|
||||
message.addRecipient(&to);
|
||||
message.addRecipient(to);
|
||||
|
||||
message.setSubject("SmtpClient for Qt - Demo");
|
||||
|
||||
|
@ -27,10 +27,10 @@ int main(int argc, char *argv[])
|
||||
MimeMessage message;
|
||||
|
||||
EmailAddress sender("your_email_address@host.com", "Your Name");
|
||||
message.setSender(&sender);
|
||||
message.setSender(sender);
|
||||
|
||||
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");
|
||||
|
||||
|
@ -21,9 +21,13 @@
|
||||
/* [1] Constructors and Destructors */
|
||||
|
||||
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()
|
||||
@ -35,23 +39,13 @@ EmailAddress::~EmailAddress()
|
||||
|
||||
/* [2] Getters and Setters */
|
||||
|
||||
void EmailAddress::setName(const QString & name)
|
||||
{
|
||||
this->name = name;
|
||||
|
||||
}
|
||||
|
||||
void EmailAddress::setAddress(const QString & address)
|
||||
{
|
||||
this->address = address;
|
||||
}
|
||||
|
||||
const QString & EmailAddress::getName() const
|
||||
QString EmailAddress::getName() const
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
const QString & EmailAddress::getAddress() const
|
||||
QString EmailAddress::getAddress() const
|
||||
{
|
||||
return address;
|
||||
}
|
||||
|
@ -28,8 +28,8 @@ public:
|
||||
|
||||
/* [1] Constructors and Destructors */
|
||||
|
||||
EmailAddress();
|
||||
EmailAddress(const QString & address, const QString & name="");
|
||||
EmailAddress(const QString & address = "", const QString & name = "");
|
||||
EmailAddress(const EmailAddress &other);
|
||||
|
||||
~EmailAddress();
|
||||
|
||||
@ -37,11 +37,9 @@ public:
|
||||
|
||||
|
||||
/* [2] Getters and Setters */
|
||||
void setName(const QString & name);
|
||||
void setAddress(const QString & address);
|
||||
|
||||
const QString & getName() const;
|
||||
const QString & getAddress() const;
|
||||
QString getAddress() const;
|
||||
QString getName() const;
|
||||
|
||||
/* [2] --- */
|
||||
|
||||
@ -50,8 +48,8 @@ private:
|
||||
|
||||
/* [3] Private members */
|
||||
|
||||
QString name;
|
||||
QString address;
|
||||
QString name;
|
||||
|
||||
/* [3] --- */
|
||||
};
|
||||
|
@ -4,7 +4,6 @@ MimeBase64Formatter::MimeBase64Formatter(QIODevice *out) :
|
||||
MimeContentFormatter(out) {}
|
||||
|
||||
qint64 MimeBase64Formatter::writeData(const char *data, qint64 maxLength) {
|
||||
qDebug("called");
|
||||
int lines = (maxLength - 1) / lineLength + 1;
|
||||
for (int i = 1; i < lines; ++i) {
|
||||
output->write(data, lineLength);
|
||||
|
@ -37,7 +37,7 @@ void MimeHtml::setHtml(const QString & html)
|
||||
this->text = html;
|
||||
}
|
||||
|
||||
const QString & MimeHtml::getHtml() const
|
||||
QString MimeHtml::getHtml() const
|
||||
{
|
||||
return text;
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ public:
|
||||
|
||||
void setHtml(const QString & html);
|
||||
|
||||
const QString& getHtml() const;
|
||||
QString getHtml() const;
|
||||
|
||||
/* [2] --- */
|
||||
|
||||
|
@ -23,7 +23,7 @@
|
||||
MimeInlineFile::MimeInlineFile(QFile *f)
|
||||
: MimeFile(f)
|
||||
{
|
||||
this->headerLines += "Content-Disposition: inline\r\n";
|
||||
addHeaderLine("Content-Disposition: inline");
|
||||
}
|
||||
|
||||
MimeInlineFile::~MimeInlineFile()
|
||||
|
@ -49,12 +49,12 @@ void MimeMessage::setContent(MimePart *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)
|
||||
{
|
||||
@ -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;
|
||||
}
|
||||
|
||||
void MimeMessage::addCc(EmailAddress* rcpt) {
|
||||
void MimeMessage::addCc(const EmailAddress &rcpt) {
|
||||
this->recipientsCc << rcpt;
|
||||
}
|
||||
|
||||
void MimeMessage::addBcc(EmailAddress* rcpt) {
|
||||
void MimeMessage::addBcc(const EmailAddress &rcpt) {
|
||||
this->recipientsBcc << rcpt;
|
||||
}
|
||||
|
||||
void MimeMessage::addCustomHeader(const QString &header)
|
||||
{
|
||||
this->customHeaders << header;
|
||||
}
|
||||
|
||||
void MimeMessage::setSubject(const QString & subject)
|
||||
{
|
||||
this->subject = subject;
|
||||
@ -99,12 +104,12 @@ void MimeMessage::setHeaderEncoding(MimePart::Encoding 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)
|
||||
{
|
||||
@ -118,7 +123,7 @@ const QList<EmailAddress*> & MimeMessage::getRecipients(RecipientType type) cons
|
||||
}
|
||||
}
|
||||
|
||||
const QString & MimeMessage::getSubject() const
|
||||
QString MimeMessage::getSubject() const
|
||||
{
|
||||
return subject;
|
||||
}
|
||||
@ -148,23 +153,30 @@ QString MimeMessage::toString() const
|
||||
return QString(out.buffer());
|
||||
}
|
||||
|
||||
QByteArray MimeMessage::formatAddress(EmailAddress *address, MimePart::Encoding encoding) {
|
||||
QByteArray MimeMessage::formatAddress(const EmailAddress &address, MimePart::Encoding encoding) {
|
||||
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)
|
||||
{
|
||||
case MimePart::Base64:
|
||||
result.append(" =?utf-8?B?" + address->getName().toLocal8Bit().toBase64() + "?=");
|
||||
result.append(" =?utf-8?B?" + text.toUtf8().toBase64() + "?=");
|
||||
break;
|
||||
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;
|
||||
default:
|
||||
result.append(" ").append(address->getName().toLocal8Bit());
|
||||
result.append(" ").append(text.toLocal8Bit());
|
||||
}
|
||||
}
|
||||
result.append(" <" + address->getAddress() + ">");
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -202,22 +214,15 @@ void MimeMessage::writeToDevice(QIODevice &out) const {
|
||||
|
||||
/* ------------ Subject ------------- */
|
||||
header.append("Subject: ");
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
header.append(format(subject, hEncoding));
|
||||
header.append("\r\n");
|
||||
/* ---------------------------------- */
|
||||
|
||||
foreach (QString hdr, customHeaders) {
|
||||
header.append(hdr.toLocal8Bit());
|
||||
header.append("\r\n");
|
||||
}
|
||||
|
||||
header.append("MIME-Version: 1.0\r\n");
|
||||
|
||||
out.write(header);
|
||||
|
@ -19,7 +19,7 @@
|
||||
#ifndef MIMEMESSAGE_H
|
||||
#define MIMEMESSAGE_H
|
||||
|
||||
#include <QList>
|
||||
#include <QStringList>
|
||||
#include <QTextStream>
|
||||
|
||||
#include "smtpmime_global.h"
|
||||
@ -39,7 +39,7 @@ public:
|
||||
|
||||
/* [1] Constructors and Destructors */
|
||||
|
||||
MimeMessage(bool createAutoMimeConent = true);
|
||||
MimeMessage(bool createAutoMimeContent = true);
|
||||
~MimeMessage();
|
||||
|
||||
/* [1] --- */
|
||||
@ -47,19 +47,21 @@ public:
|
||||
|
||||
/* [2] Getters and Setters */
|
||||
|
||||
void setSender(EmailAddress* e);
|
||||
void addRecipient(EmailAddress* rcpt, RecipientType type = To);
|
||||
void addTo(EmailAddress* rcpt);
|
||||
void addCc(EmailAddress* rcpt);
|
||||
void addBcc(EmailAddress* rcpt);
|
||||
void setSender(const EmailAddress &sndr);
|
||||
void addRecipient(const EmailAddress &rcpt, RecipientType type = To);
|
||||
void addTo(const EmailAddress &rcpt);
|
||||
void addCc(const EmailAddress &rcpt);
|
||||
void addBcc(const EmailAddress &rcpt);
|
||||
void addCustomHeader(const QString &hdr);
|
||||
void setSubject(const QString &subject);
|
||||
void addPart(MimePart* part);
|
||||
|
||||
void setHeaderEncoding(MimePart::Encoding);
|
||||
|
||||
const EmailAddress & getSender() const;
|
||||
const QList<EmailAddress*> & getRecipients(RecipientType type = To) const;
|
||||
const QString & getSubject() const;
|
||||
EmailAddress getSender() const;
|
||||
const QList<EmailAddress> &getRecipients(RecipientType type = To) const;
|
||||
QString getSubject() const;
|
||||
const QStringList &getCustomHeaders() const;
|
||||
const QList<MimePart*> & getParts() const;
|
||||
|
||||
MimePart& getContent();
|
||||
@ -78,14 +80,16 @@ protected:
|
||||
|
||||
/* [4] Protected members */
|
||||
|
||||
EmailAddress* sender;
|
||||
QList<EmailAddress*> recipientsTo, recipientsCc, recipientsBcc;
|
||||
EmailAddress sender;
|
||||
QList<EmailAddress> recipientsTo, recipientsCc, recipientsBcc;
|
||||
QString subject;
|
||||
QStringList customHeaders;
|
||||
MimePart *content;
|
||||
|
||||
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] --- */
|
||||
|
||||
|
@ -43,7 +43,9 @@ MimeMultiPart::MimeMultiPart(MultiPartType type)
|
||||
}
|
||||
|
||||
MimeMultiPart::~MimeMultiPart() {
|
||||
|
||||
foreach (MimePart *part, parts) {
|
||||
delete part;
|
||||
}
|
||||
}
|
||||
|
||||
void MimeMultiPart::addPart(MimePart *part) {
|
||||
|
@ -58,12 +58,12 @@ void MimePart::addHeaderLine(const QString & line)
|
||||
this->headerLines += line + "\r\n";
|
||||
}
|
||||
|
||||
const QString& MimePart::getHeader() const
|
||||
QString MimePart::getHeader() const
|
||||
{
|
||||
return headerLines;
|
||||
}
|
||||
|
||||
const QByteArray& MimePart::getContent() const
|
||||
QByteArray MimePart::getContent() const
|
||||
{
|
||||
return content;
|
||||
}
|
||||
@ -73,7 +73,7 @@ void MimePart::setContentId(const QString & cId)
|
||||
this->cId = cId;
|
||||
}
|
||||
|
||||
const QString & MimePart::getContentId() const
|
||||
QString MimePart::getContentId() const
|
||||
{
|
||||
return this->cId;
|
||||
}
|
||||
@ -83,7 +83,7 @@ void MimePart::setContentName(const QString & cName)
|
||||
this->cName = cName;
|
||||
}
|
||||
|
||||
const QString & MimePart::getContentName() const
|
||||
QString MimePart::getContentName() const
|
||||
{
|
||||
return this->cName;
|
||||
}
|
||||
@ -93,7 +93,7 @@ void MimePart::setContentType(const QString & cType)
|
||||
this->cType = cType;
|
||||
}
|
||||
|
||||
const QString & MimePart::getContentType() const
|
||||
QString MimePart::getContentType() const
|
||||
{
|
||||
return this->cType;
|
||||
}
|
||||
@ -103,7 +103,7 @@ void MimePart::setCharset(const QString & charset)
|
||||
this->cCharset = charset;
|
||||
}
|
||||
|
||||
const QString & MimePart::getCharset() const
|
||||
QString MimePart::getCharset() const
|
||||
{
|
||||
return this->cCharset;
|
||||
}
|
||||
|
@ -51,25 +51,25 @@ public:
|
||||
|
||||
/* [2] Getters and Setters */
|
||||
|
||||
const QString& getHeader() const;
|
||||
const QByteArray& getContent() const;
|
||||
|
||||
void setContent(const QByteArray & content);
|
||||
QByteArray getContent() const;
|
||||
|
||||
void setHeader(const QString & headerLines);
|
||||
QString getHeader() const;
|
||||
|
||||
void addHeaderLine(const QString & line);
|
||||
|
||||
void setContentId(const QString & cId);
|
||||
const QString & getContentId() const;
|
||||
QString getContentId() const;
|
||||
|
||||
void setContentName(const QString & cName);
|
||||
const QString & getContentName() const;
|
||||
QString getContentName() const;
|
||||
|
||||
void setContentType(const QString & cType);
|
||||
const QString & getContentType() const;
|
||||
QString getContentType() const;
|
||||
|
||||
void setCharset(const QString & charset);
|
||||
const QString & getCharset() const;
|
||||
QString getCharset() const;
|
||||
|
||||
void setEncoding(Encoding enc);
|
||||
Encoding getEncoding() const;
|
||||
|
@ -33,7 +33,8 @@ SmtpClient::SmtpClient(const QString & host, int port, ConnectionType connection
|
||||
name("localhost"),
|
||||
isReadyConnected(false),
|
||||
isAuthenticated(false),
|
||||
isMailSent(false)
|
||||
isMailSent(false),
|
||||
isReset(false)
|
||||
{
|
||||
setConnectionType(connectionType);
|
||||
|
||||
@ -54,7 +55,7 @@ SmtpClient::~SmtpClient() {}
|
||||
/**
|
||||
* @brief Returns the host name of the server.
|
||||
*/
|
||||
const QString& SmtpClient::getHost() const
|
||||
QString SmtpClient::getHost() const
|
||||
{
|
||||
return this->host;
|
||||
}
|
||||
@ -78,7 +79,7 @@ SmtpClient::ConnectionType SmtpClient::getConnectionType() const
|
||||
/**
|
||||
* @brief Returns the client's name.
|
||||
*/
|
||||
const QString& SmtpClient::getName() const
|
||||
QString SmtpClient::getName() const
|
||||
{
|
||||
return this->name;
|
||||
}
|
||||
@ -94,7 +95,7 @@ void SmtpClient::setName(const QString &name)
|
||||
/**
|
||||
* @brief Returns the last response of the server.
|
||||
*/
|
||||
const QString & SmtpClient::getResponseText() const
|
||||
QString SmtpClient::getResponseText() const
|
||||
{
|
||||
return responseText;
|
||||
}
|
||||
@ -160,19 +161,24 @@ void SmtpClient::quit()
|
||||
changeState(DisconnectingState);
|
||||
}
|
||||
|
||||
void SmtpClient::reset()
|
||||
{
|
||||
if (!isReadyConnected)
|
||||
return;
|
||||
|
||||
isReset = false;
|
||||
|
||||
changeState(ResetState);
|
||||
}
|
||||
|
||||
bool SmtpClient::waitForReadyConnected(int msec) {
|
||||
if (state == UnconnectedState)
|
||||
return false;
|
||||
|
||||
QEventLoop loop;
|
||||
QObject::connect(this, SIGNAL(readyConnected()), &loop, SLOT(quit()));
|
||||
|
||||
if (isReadyConnected)
|
||||
return true;
|
||||
|
||||
QTimer::singleShot(msec, &loop, SLOT(quit()));
|
||||
loop.exec();
|
||||
waitForEvent(msec, SIGNAL(readyConnected()));
|
||||
|
||||
return isReadyConnected;
|
||||
}
|
||||
@ -181,14 +187,10 @@ bool SmtpClient::waitForAuthenticated(int msec) {
|
||||
if (!isReadyConnected)
|
||||
return false;
|
||||
|
||||
QEventLoop loop;
|
||||
QObject::connect(this, SIGNAL(authenticated()), &loop, SLOT(quit()));
|
||||
|
||||
if (isAuthenticated)
|
||||
return true;
|
||||
|
||||
QTimer::singleShot(msec, &loop, SLOT(quit()));
|
||||
loop.exec();
|
||||
waitForEvent(msec, SIGNAL(authenticated()));
|
||||
|
||||
return isAuthenticated;
|
||||
}
|
||||
@ -197,18 +199,27 @@ bool SmtpClient::waitForMailSent(int msec) {
|
||||
if (!isReadyConnected)
|
||||
return false;
|
||||
|
||||
QEventLoop loop;
|
||||
QObject::connect(this, SIGNAL(mailSent()), &loop, SLOT(quit()));
|
||||
|
||||
if (isMailSent)
|
||||
return true;
|
||||
|
||||
QTimer::singleShot(msec, &loop, SLOT(quit()));
|
||||
loop.exec();
|
||||
waitForEvent(msec, SIGNAL(mailSent()));
|
||||
|
||||
return isMailSent;
|
||||
}
|
||||
|
||||
bool SmtpClient::waitForReset(int msec)
|
||||
{
|
||||
if (!isReadyConnected)
|
||||
return false;
|
||||
|
||||
if (isReset)
|
||||
return true;
|
||||
|
||||
waitForEvent(msec, SIGNAL(mailReset()));
|
||||
|
||||
return isReset;
|
||||
}
|
||||
|
||||
/* [3] --- */
|
||||
|
||||
|
||||
@ -276,8 +287,12 @@ void SmtpClient::changeState(SmtpClient::ClientState state) {
|
||||
socket->disconnectFromHost();
|
||||
break;
|
||||
|
||||
case ResetState:
|
||||
sendMessage("RSET");
|
||||
break;
|
||||
|
||||
case _EHLO_State:
|
||||
// Service ready. Send EHLO message and chage the state
|
||||
// Service ready. Send EHLO message and change the state
|
||||
sendMessage("EHLO " + name);
|
||||
break;
|
||||
|
||||
@ -345,7 +360,7 @@ void SmtpClient::changeState(SmtpClient::ClientState state) {
|
||||
|
||||
case _MAIL_1_RCPT_INIT:
|
||||
rcptType++;
|
||||
const QList<EmailAddress*> *addressList;
|
||||
const QList<EmailAddress> *addressList;
|
||||
switch (rcptType)
|
||||
{
|
||||
case _TO:
|
||||
@ -368,7 +383,7 @@ void SmtpClient::changeState(SmtpClient::ClientState state) {
|
||||
|
||||
case _MAIL_2_RCPT:
|
||||
if (addressIt != addressItEnd) {
|
||||
sendMessage("RCPT TO: <" + (*addressIt)->getAddress() + ">");
|
||||
sendMessage("RCPT TO: <" + addressIt->getAddress() + ">");
|
||||
addressIt++;
|
||||
} else {
|
||||
changeState(_MAIL_1_RCPT_INIT);
|
||||
@ -407,15 +422,26 @@ void SmtpClient::processResponse() {
|
||||
case ConnectedState:
|
||||
// Just connected to the server. Wait for 220 (Service ready)
|
||||
if (responseCode != 220) {
|
||||
emit error(ServerError); return;
|
||||
emitError(ServerError);
|
||||
return;
|
||||
}
|
||||
changeState(_EHLO_State);
|
||||
break;
|
||||
|
||||
case ResetState:
|
||||
if (responseCode != 250) {
|
||||
emitError(ServerError);
|
||||
return;
|
||||
}
|
||||
emit mailReset();
|
||||
changeState(ReadyState);
|
||||
break;
|
||||
|
||||
case _EHLO_State:
|
||||
// The response code needs to be 250.
|
||||
if (responseCode != 250) {
|
||||
emit error(ServerError); return;
|
||||
emitError(ServerError);
|
||||
return;
|
||||
}
|
||||
|
||||
changeState((connectionType != TlsConnection) ? _READY_Connected : _TLS_State);
|
||||
@ -425,7 +451,7 @@ void SmtpClient::processResponse() {
|
||||
case _TLS_0_STARTTLS:
|
||||
// The response code needs to be 220.
|
||||
if (responseCode != 220) {
|
||||
emit error(ServerError);
|
||||
emitError(ServerError);
|
||||
return;
|
||||
}
|
||||
changeState(_TLS_1_ENCRYPT);
|
||||
@ -434,7 +460,7 @@ void SmtpClient::processResponse() {
|
||||
case _TLS_2_EHLO:
|
||||
// The response code needs to be 250.
|
||||
if (responseCode != 250) {
|
||||
emit error(ServerError);
|
||||
emitError(ServerError);
|
||||
return;
|
||||
}
|
||||
changeState(_READY_Encrypted);
|
||||
@ -444,7 +470,7 @@ void SmtpClient::processResponse() {
|
||||
case _AUTH_PLAIN_0:
|
||||
// If the response is not 235 then the authentication was failed
|
||||
if (responseCode != 235) {
|
||||
emit error(AuthenticationError);
|
||||
emitError(AuthenticationError);
|
||||
return;
|
||||
}
|
||||
changeState(_READY_Authenticated);
|
||||
@ -452,7 +478,7 @@ void SmtpClient::processResponse() {
|
||||
|
||||
case _AUTH_LOGIN_0:
|
||||
if (responseCode != 334) {
|
||||
emit error(AuthenticationError);
|
||||
emitError(AuthenticationError);
|
||||
return;
|
||||
}
|
||||
changeState(_AUTH_LOGIN_1_USER);
|
||||
@ -460,7 +486,7 @@ void SmtpClient::processResponse() {
|
||||
|
||||
case _AUTH_LOGIN_1_USER:
|
||||
if (responseCode != 334) {
|
||||
emit error(AuthenticationError);
|
||||
emitError(AuthenticationError);
|
||||
return;
|
||||
}
|
||||
changeState(_AUTH_LOGIN_2_PASS);
|
||||
@ -468,7 +494,7 @@ void SmtpClient::processResponse() {
|
||||
|
||||
case _AUTH_LOGIN_2_PASS:
|
||||
if (responseCode != 235) {
|
||||
emit error(AuthenticationError);
|
||||
emitError(AuthenticationError);
|
||||
return;
|
||||
}
|
||||
changeState(_READY_Authenticated);
|
||||
@ -477,7 +503,7 @@ void SmtpClient::processResponse() {
|
||||
/* --- MAIL --- */
|
||||
case _MAIL_0_FROM:
|
||||
if (responseCode != 250) {
|
||||
emit error(MailSendingError);
|
||||
emitError(MailSendingError);
|
||||
return;
|
||||
}
|
||||
changeState(_MAIL_1_RCPT_INIT);
|
||||
@ -485,7 +511,7 @@ void SmtpClient::processResponse() {
|
||||
|
||||
case _MAIL_2_RCPT:
|
||||
if (responseCode != 250) {
|
||||
emit error(MailSendingError);
|
||||
emitError(MailSendingError);
|
||||
return;
|
||||
}
|
||||
changeState(_MAIL_2_RCPT);
|
||||
@ -493,7 +519,7 @@ void SmtpClient::processResponse() {
|
||||
|
||||
case _MAIL_3_DATA:
|
||||
if (responseCode != 354) {
|
||||
emit error(MailSendingError);
|
||||
emitError(MailSendingError);
|
||||
return;
|
||||
}
|
||||
changeState(_MAIL_4_SEND_DATA);
|
||||
@ -501,7 +527,7 @@ void SmtpClient::processResponse() {
|
||||
|
||||
case _MAIL_4_SEND_DATA:
|
||||
if (responseCode != 250) {
|
||||
emit error(MailSendingError);
|
||||
emitError(MailSendingError);
|
||||
return;
|
||||
}
|
||||
changeState(_READY_MailSent);
|
||||
@ -523,6 +549,28 @@ void SmtpClient::sendMessage(const QString &text)
|
||||
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] --- */
|
||||
|
||||
|
||||
@ -551,8 +599,6 @@ void SmtpClient::socketStateChanged(QAbstractSocket::SocketState state) {
|
||||
void SmtpClient::socketError(QAbstractSocket::SocketError socketError) {
|
||||
#ifndef QT_NO_DEBUG
|
||||
qDebug() << "[Socket] ERROR:" << socketError;
|
||||
#else
|
||||
Q_UNUSED(socketError);
|
||||
#endif
|
||||
emit error(SocketError);
|
||||
}
|
||||
@ -583,13 +629,13 @@ void SmtpClient::socketReadyRead()
|
||||
|
||||
// Check for server error
|
||||
if (responseCode / 100 == 4) {
|
||||
emit error(ServerError);
|
||||
emitError(ServerError);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for client error
|
||||
if (responseCode / 100 == 5) {
|
||||
emit error(ClientError);
|
||||
emitError(ClientError);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -66,6 +66,7 @@ public:
|
||||
AuthenticatingState = 4,
|
||||
MailSendingState = 5,
|
||||
DisconnectingState = 6,
|
||||
ResetState = 7,
|
||||
|
||||
/* Internal States */
|
||||
_EHLO_State = 50,
|
||||
@ -102,7 +103,7 @@ public:
|
||||
|
||||
/* [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();
|
||||
|
||||
@ -111,14 +112,14 @@ public:
|
||||
|
||||
/* [2] Getters and Setters */
|
||||
|
||||
const QString& getHost() const;
|
||||
QString getHost() const;
|
||||
int getPort() const;
|
||||
ConnectionType getConnectionType() const;
|
||||
|
||||
const QString& getName() const;
|
||||
QString getName() const;
|
||||
void setName(const QString &name);
|
||||
|
||||
const QString & getResponseText() const;
|
||||
QString getResponseText() const;
|
||||
int getResponseCode() const;
|
||||
|
||||
QTcpSocket* getSocket();
|
||||
@ -132,10 +133,15 @@ public:
|
||||
void login(const QString &user, const QString &password, AuthMethod method = AuthLogin);
|
||||
void sendMail(const MimeMessage & email);
|
||||
void quit();
|
||||
void reset();
|
||||
|
||||
bool isConnected();
|
||||
bool isLogged();
|
||||
|
||||
bool waitForReadyConnected(int msec = 30000);
|
||||
bool waitForAuthenticated(int msec = 30000);
|
||||
bool waitForMailSent(int msec = 30000);
|
||||
bool waitForReset(int msec = 30000);
|
||||
|
||||
/* [3] --- */
|
||||
|
||||
@ -170,14 +176,15 @@ protected:
|
||||
bool isReadyConnected;
|
||||
bool isAuthenticated;
|
||||
bool isMailSent;
|
||||
bool isReset;
|
||||
|
||||
const MimeMessage *email;
|
||||
|
||||
int rcptType;
|
||||
enum _RcptType { _TO = 1, _CC = 2, _BCC = 3};
|
||||
|
||||
QList<EmailAddress*>::const_iterator addressIt;
|
||||
QList<EmailAddress*>::const_iterator addressItEnd;
|
||||
QList<EmailAddress>::const_iterator addressIt;
|
||||
QList<EmailAddress>::const_iterator addressItEnd;
|
||||
|
||||
/* [4] --- */
|
||||
|
||||
@ -188,6 +195,8 @@ protected:
|
||||
void changeState(ClientState state);
|
||||
void processResponse();
|
||||
void sendMessage(const QString &text);
|
||||
void emitError(SmtpClient::SmtpError e);
|
||||
void waitForEvent(int msec, const char *successSignal);
|
||||
|
||||
/* [5] --- */
|
||||
|
||||
@ -213,6 +222,7 @@ signals:
|
||||
void readyConnected();
|
||||
void authenticated();
|
||||
void mailSent();
|
||||
void mailReset();
|
||||
void disconnected();
|
||||
|
||||
/* [7] --- */
|
||||
|
Loading…
Reference in New Issue
Block a user