Merge pull request #17 from LascauxSRL/master

Added support for ByteArray Attachments and removed Memory Leak from MimeMessage class
This commit is contained in:
Tőkés Attila 2014-02-21 17:18:51 +02:00
commit 076034a612
8 changed files with 73 additions and 8 deletions

View File

@ -24,6 +24,10 @@
MimeAttachment::MimeAttachment(QFile *file) MimeAttachment::MimeAttachment(QFile *file)
: MimeFile(file) : MimeFile(file)
{ {
}
MimeAttachment::MimeAttachment(const QByteArray& stream, const QString& fileName): MimeFile(stream, fileName)
{
} }
MimeAttachment::~MimeAttachment() MimeAttachment::~MimeAttachment()

View File

@ -31,6 +31,8 @@ public:
/* [1] Constructors and Destructors */ /* [1] Constructors and Destructors */
MimeAttachment(QFile* file); MimeAttachment(QFile* file);
MimeAttachment(const QByteArray& stream, const QString& fileName);
~MimeAttachment(); ~MimeAttachment();
/* [1] --- */ /* [1] --- */

View File

@ -28,9 +28,18 @@ MimeFile::MimeFile(QFile *file)
this->cName = QFileInfo(*file).fileName(); this->cName = QFileInfo(*file).fileName();
this->cEncoding = Base64; 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() MimeFile::~MimeFile()
{ {
if (file)
delete file; delete file;
} }
@ -45,11 +54,13 @@ MimeFile::~MimeFile()
/* [3] Protected methods */ /* [3] Protected methods */
void MimeFile::prepare() void MimeFile::prepare()
{
if (this->file)
{ {
file->open(QIODevice::ReadOnly); file->open(QIODevice::ReadOnly);
this->content = file->readAll(); this->content = file->readAll();
file->close(); file->close();
}
/* !!! IMPORTANT !!!! */ /* !!! IMPORTANT !!!! */
MimePart::prepare(); MimePart::prepare();
} }

View File

@ -29,6 +29,7 @@ public:
/* [1] Constructors and Destructors */ /* [1] Constructors and Destructors */
MimeFile(const QByteArray& stream, const QString& fileName);
MimeFile(QFile *f); MimeFile(QFile *f);
~MimeFile(); ~MimeFile();

View File

@ -23,16 +23,22 @@
#include <typeinfo> #include <typeinfo>
/* [1] Constructors and Destructors */ /* [1] Constructors and Destructors */
MimeMessage::MimeMessage(bool createAutoMimeContent) : MimeMessage::MimeMessage(bool createAutoMimeContent) :
hEncoding(MimePart::_8Bit) hEncoding(MimePart::_8Bit)
{ {
if (createAutoMimeContent) if (createAutoMimeContent)
this->content = new MimeMultiPart(); this->content = new MimeMultiPart();
autoMimeContentCreated = createAutoMimeContent;
} }
MimeMessage::~MimeMessage() MimeMessage::~MimeMessage()
{ {
if (this->autoMimeContentCreated)
{
this->autoMimeContentCreated = false;
delete (this->content);
}
} }
/* [1] --- */ /* [1] --- */
@ -44,6 +50,11 @@ MimePart& MimeMessage::getContent() {
} }
void MimeMessage::setContent(MimePart *content) { void MimeMessage::setContent(MimePart *content) {
if (this->autoMimeContentCreated)
{
this->autoMimeContentCreated = false;
delete (this->content);
}
this->content = content; this->content = content;
} }

View File

@ -78,6 +78,7 @@ protected:
QList<EmailAddress*> recipientsTo, recipientsCc, recipientsBcc; QList<EmailAddress*> recipientsTo, recipientsCc, recipientsBcc;
QString subject; QString subject;
MimePart *content; MimePart *content;
bool autoMimeContentCreated;
MimePart::Encoding hEncoding; MimePart::Encoding hEncoding;

View File

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

View File

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