From 93873f8b1c03cefd42d5671317a0f10ae82c29cd Mon Sep 17 00:00:00 2001 From: LascauxSRL Date: Thu, 8 Aug 2013 10:45:39 +0200 Subject: [PATCH 1/2] - Added support for ByteArray Attachments - Removed memoryleak from MimeMessage --- src/mimeattachment.cpp | 4 ++++ src/mimeattachment.h | 2 ++ src/mimefile.cpp | 13 ++++++++++++- src/mimefile.h | 1 + src/mimemessage.cpp | 13 ++++++++++++- src/mimemessage.h | 3 ++- 6 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/mimeattachment.cpp b/src/mimeattachment.cpp index eac36f7..0dea622 100644 --- a/src/mimeattachment.cpp +++ b/src/mimeattachment.cpp @@ -24,6 +24,10 @@ MimeAttachment::MimeAttachment(QFile *file) : MimeFile(file) { +} +MimeAttachment::MimeAttachment(const QByteArray& stream, const QString& fileName): MimeFile(stream, fileName) +{ + } MimeAttachment::~MimeAttachment() diff --git a/src/mimeattachment.h b/src/mimeattachment.h index 4b79678..4f9efa0 100644 --- a/src/mimeattachment.h +++ b/src/mimeattachment.h @@ -31,6 +31,8 @@ public: /* [1] Constructors and Destructors */ MimeAttachment(QFile* file); + MimeAttachment(const QByteArray& stream, const QString& fileName); + ~MimeAttachment(); /* [1] --- */ diff --git a/src/mimefile.cpp b/src/mimefile.cpp index 59fb5bf..4991697 100644 --- a/src/mimefile.cpp +++ b/src/mimefile.cpp @@ -28,9 +28,18 @@ MimeFile::MimeFile(QFile *file) this->cName = QFileInfo(*file).fileName(); this->cEncoding = Base64; } +MimeFile::MimeFile(const QByteArray& stream, const QString& fileName) +{ + this->cEncoding = Base64; + this->cType = "application/octet-stream"; + this->file = 0; + this->cName = fileName; + this->content = stream; +} MimeFile::~MimeFile() { + if (file) delete file; } @@ -46,10 +55,12 @@ MimeFile::~MimeFile() void MimeFile::prepare() { + if (this->file) + { file->open(QIODevice::ReadOnly); this->content = file->readAll(); file->close(); - + } /* !!! IMPORTANT !!!! */ MimePart::prepare(); } diff --git a/src/mimefile.h b/src/mimefile.h index b72e947..9e1b46f 100644 --- a/src/mimefile.h +++ b/src/mimefile.h @@ -29,6 +29,7 @@ public: /* [1] Constructors and Destructors */ + MimeFile(const QByteArray& stream, const QString& fileName); MimeFile(QFile *f); ~MimeFile(); diff --git a/src/mimemessage.cpp b/src/mimemessage.cpp index 14a76c7..0c4ebc2 100644 --- a/src/mimemessage.cpp +++ b/src/mimemessage.cpp @@ -23,16 +23,22 @@ #include /* [1] Constructors and Destructors */ - MimeMessage::MimeMessage(bool createAutoMimeContent) : hEncoding(MimePart::_8Bit) { if (createAutoMimeContent) this->content = new MimeMultiPart(); + + autoMimeContentCreated = createAutoMimeContent; } MimeMessage::~MimeMessage() { + if (this->autoMimeContentCreated) + { + this->autoMimeContentCreated = false; + delete (this->content); + } } /* [1] --- */ @@ -44,6 +50,11 @@ MimePart& MimeMessage::getContent() { } void MimeMessage::setContent(MimePart *content) { + if (this->autoMimeContentCreated) + { + this->autoMimeContentCreated = false; + delete (this->content); + } this->content = content; } diff --git a/src/mimemessage.h b/src/mimemessage.h index d0297d1..b524f74 100644 --- a/src/mimemessage.h +++ b/src/mimemessage.h @@ -78,7 +78,8 @@ protected: QList recipientsTo, recipientsCc, recipientsBcc; QString subject; MimePart *content; - + bool autoMimeContentCreated; + MimePart::Encoding hEncoding; /* [4] --- */ From 4f028793e1bf9cca2871f93992ab97a85c463f30 Mon Sep 17 00:00:00 2001 From: LascauxSRL Date: Thu, 20 Feb 2014 11:41:21 +0100 Subject: [PATCH 2/2] - Added support for SendMessage Timeout (Doesn't rely on Reply Timeout) - Solved small bug in emitting an error message --- src/smtpclient.cpp | 34 +++++++++++++++++++++++++++++++--- src/smtpclient.h | 11 +++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/smtpclient.cpp b/src/smtpclient.cpp index 2071d8d..8035324 100644 --- a/src/smtpclient.cpp +++ b/src/smtpclient.cpp @@ -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] --- */ diff --git a/src/smtpclient.h b/src/smtpclient.h index 96ab77a..5e3372c 100644 --- a/src/smtpclient.h +++ b/src/smtpclient.h @@ -42,6 +42,7 @@ public: { ConnectionTimeoutError, ResponseTimeoutError, + SendDataTimeoutError, AuthenticationFailedError, ServerError, // 4xx smtp error ClientError // 5xx smtp error @@ -97,6 +98,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] --- */