Compare commits
44 Commits
Author | SHA1 | Date | |
---|---|---|---|
39d6f35ca7 | |||
|
3fa4a0fe57 | ||
|
152a3e7572 | ||
|
f715bb9916 | ||
|
67c73c7c96 | ||
|
1f4f2e208b | ||
|
5bdfa6a5dd | ||
|
3d87c6306e | ||
|
81c652dc73 | ||
|
ca43f9d642 | ||
|
12c810ada9 | ||
|
7633337dd6 | ||
|
5fc59c0863 | ||
|
6b676827fe | ||
|
cb04e35bed | ||
|
96452106d8 | ||
|
8623093e9b | ||
|
800ff9cf69 | ||
|
ff2eb297c4 | ||
|
ef6194fb26 | ||
|
78bf2dac42 | ||
|
c2a734791a | ||
|
48c080b9ee | ||
|
6de3493c92 | ||
|
9afc349942 | ||
|
c12f70b721 | ||
|
6877ac8148 | ||
|
f8db82bae5 | ||
|
7f8d11db2f | ||
|
06db4130d9 | ||
|
235a350a34 | ||
|
75f4bf385a | ||
|
a4f4235139 | ||
|
649ea30336 | ||
|
076034a612 | ||
|
b1c640c39d | ||
|
4f028793e1 | ||
|
f3da3b391c | ||
|
93873f8b1c | ||
|
dac7a04d6f | ||
|
8f6a67664f | ||
|
3d3f9136af | ||
|
81e561c91b | ||
|
32b11e2351 |
@ -3,7 +3,7 @@ SMTP Client for Qt (C++) - Version 1.1
|
||||
|
||||
The SmtpClient for Qt is small library writen for Qt 4 (C++ version) that allows application to send complex emails (plain text, html, attachments, inline files, etc.) using the Simple Mail Transfer Protocol (SMTP).
|
||||
|
||||
##New in version 1.1:
|
||||
## New in version 1.1:
|
||||
|
||||
- TLS (STARTTLS) connection is now supported
|
||||
|
||||
|
@ -4,11 +4,17 @@
|
||||
#
|
||||
#-------------------------------------------------
|
||||
|
||||
QT += core gui network
|
||||
QT += core network
|
||||
|
||||
TARGET = SMTPEmail
|
||||
TEMPLATE = app
|
||||
|
||||
# Build as an application
|
||||
#TEMPLATE = app
|
||||
|
||||
# Build as a library
|
||||
TEMPLATE = lib
|
||||
DEFINES += SMTP_BUILD
|
||||
win32:CONFIG += dll
|
||||
|
||||
SOURCES += \
|
||||
src/emailaddress.cpp \
|
||||
@ -22,7 +28,7 @@ SOURCES += \
|
||||
src/smtpclient.cpp \
|
||||
src/quotedprintable.cpp \
|
||||
src/mimemultipart.cpp \
|
||||
src/mimecontentformatter.cpp
|
||||
src/mimecontentformatter.cpp \
|
||||
|
||||
HEADERS += \
|
||||
src/emailaddress.h \
|
||||
@ -37,7 +43,8 @@ HEADERS += \
|
||||
src/SmtpMime \
|
||||
src/quotedprintable.h \
|
||||
src/mimemultipart.h \
|
||||
src/mimecontentformatter.h
|
||||
src/mimecontentformatter.h \
|
||||
src/smtpexports.h
|
||||
|
||||
OTHER_FILES += \
|
||||
LICENSE \
|
||||
|
@ -1,11 +1,10 @@
|
||||
#include <QtGui/QApplication>
|
||||
|
||||
#include "../src/SmtpMime"
|
||||
#include <QtCore>
|
||||
|
||||
#include "../../src/SmtpMime"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
QCoreApplication a(argc, argv);
|
||||
|
||||
// This is a first demo application of the SmtpClient for Qt project
|
||||
|
||||
@ -18,17 +17,20 @@ int main(int argc, char *argv[])
|
||||
// We need to set the username (your email address) and password
|
||||
// for smtp authentification.
|
||||
|
||||
smtp.setUser("your_email_address@gmail.com");
|
||||
smtp.setUser("your_email_address@host.com");
|
||||
smtp.setPassword("your_password");
|
||||
|
||||
// Now we create a MimeMessage object. This is the email.
|
||||
|
||||
MimeMessage message;
|
||||
|
||||
message.setSender(new EmailAddress("your_email_address@gmail.com", "Your Name"));
|
||||
message.addRecipient(new EmailAddress("recipient@host.com", "Recipient's Name"));
|
||||
message.setSubject("SmtpClient for Qt - Demo");
|
||||
EmailAddress sender("your_email_address@host.com", "Your Name");
|
||||
message.setSender(&sender);
|
||||
|
||||
EmailAddress to("recipient@host.com", "Recipient's Name");
|
||||
message.addRecipient(&to);
|
||||
|
||||
message.setSubject("SmtpClient for Qt - Demo");
|
||||
|
||||
// Now add some text to the email.
|
||||
// First we create a MimeText object.
|
||||
@ -41,12 +43,23 @@ int main(int argc, char *argv[])
|
||||
|
||||
message.addPart(&text);
|
||||
|
||||
|
||||
// Now we can send the mail
|
||||
|
||||
smtp.connectToHost();
|
||||
smtp.login();
|
||||
smtp.sendMail(message);
|
||||
if (!smtp.connectToHost()) {
|
||||
qDebug() << "Failed to connect to host!" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!smtp.login()) {
|
||||
qDebug() << "Failed to login!" << endl;
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (!smtp.sendMail(message)) {
|
||||
qDebug() << "Failed to send mail!" << endl;
|
||||
return -3;
|
||||
}
|
||||
|
||||
smtp.quit();
|
||||
|
||||
}
|
29
demos/demo1/demo1.pro
Normal file
29
demos/demo1/demo1.pro
Normal file
@ -0,0 +1,29 @@
|
||||
#-------------------------------------------------
|
||||
#
|
||||
# Project created by QtCreator 2014-10-30T22:19:03
|
||||
#
|
||||
#-------------------------------------------------
|
||||
|
||||
QT += core
|
||||
|
||||
QT -= gui
|
||||
|
||||
TARGET = demo1
|
||||
CONFIG += console
|
||||
CONFIG -= app_bundle
|
||||
|
||||
TEMPLATE = app
|
||||
|
||||
|
||||
SOURCES += \
|
||||
demo1.cpp
|
||||
|
||||
# Location of SMTP Library
|
||||
SMTP_LIBRARY_LOCATION = $$PWD/../../../build/SMTPEmail-Desktop-Debug
|
||||
|
||||
win32:CONFIG(release, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/release/ -lSMTPEmail
|
||||
else:win32:CONFIG(debug, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/debug/ -lSMTPEmail
|
||||
else:unix: LIBS += -L$$SMTP_LIBRARY_LOCATION -lSMTPEmail
|
||||
|
||||
INCLUDEPATH += $$SMTP_LIBRARY_LOCATION
|
||||
DEPENDPATH += $$SMTP_LIBRARY_LOCATION
|
@ -14,12 +14,11 @@
|
||||
See the LICENSE file for more details.
|
||||
*/
|
||||
|
||||
#include <QtGui/QApplication>
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "sendemail.h"
|
||||
#include "../../src/SmtpMime"
|
||||
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
32
demos/demo2/demo2.pro
Normal file
32
demos/demo2/demo2.pro
Normal file
@ -0,0 +1,32 @@
|
||||
#-------------------------------------------------
|
||||
#
|
||||
# Project created by QtCreator 2014-10-30T22:48:32
|
||||
#
|
||||
#-------------------------------------------------
|
||||
|
||||
QT += core gui
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
TARGET = demo2
|
||||
TEMPLATE = app
|
||||
|
||||
SOURCES += \
|
||||
demo2.cpp \
|
||||
sendemail.cpp
|
||||
|
||||
# Location of SMTP Library
|
||||
SMTP_LIBRARY_LOCATION = $$PWD/../../../build/SMTPEmail-Desktop-Debug
|
||||
|
||||
win32:CONFIG(release, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/release/ -lSMTPEmail
|
||||
else:win32:CONFIG(debug, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/debug/ -lSMTPEmail
|
||||
else:unix: LIBS += -L$$SMTP_LIBRARY_LOCATION -lSMTPEmail
|
||||
|
||||
INCLUDEPATH += $$SMTP_LIBRARY_LOCATION
|
||||
DEPENDPATH += $$SMTP_LIBRARY_LOCATION
|
||||
|
||||
HEADERS += \
|
||||
sendemail.h
|
||||
|
||||
FORMS += \
|
||||
sendemail.ui
|
@ -123,7 +123,7 @@ void SendEmail::on_sendEmail_clicked()
|
||||
else
|
||||
{
|
||||
QMessageBox okMessage (this);
|
||||
okMessage.setText("The email was succesfully sended.");
|
||||
okMessage.setText("The email was succesfully sent.");
|
||||
okMessage.exec();
|
||||
}
|
||||
|
||||
|
@ -14,26 +14,31 @@
|
||||
See the LICENSE file for more details.
|
||||
*/
|
||||
|
||||
#include <QtGui/QApplication>
|
||||
#include "../src/SmtpMime"
|
||||
#include <QtCore>
|
||||
|
||||
#include "../../src/SmtpMime"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
QCoreApplication a(argc, argv);
|
||||
|
||||
// First create the SmtpClient object and set the user and the password.
|
||||
|
||||
SmtpClient smtp("smtp.gmail.com", 465, SmtpClient::SslConnection);
|
||||
|
||||
smtp.setUser("your_email@gmail.com");
|
||||
smtp.setUser("your_email@host.com");
|
||||
smtp.setPassword("your_password");
|
||||
|
||||
// Create a MimeMessage
|
||||
|
||||
MimeMessage message;
|
||||
|
||||
message.setSender(new EmailAddress("your_email@gmail.com", "Your Name"));
|
||||
message.addRecipient(new EmailAddress("recipient@host.com", "Recipient's Name"));
|
||||
EmailAddress sender("your_email_address@host.com", "Your Name");
|
||||
message.setSender(&sender);
|
||||
|
||||
EmailAddress to("recipient@host.com", "Recipient's Name");
|
||||
message.addRecipient(&to);
|
||||
|
||||
message.setSubject("SmtpClient for Qt - Demo");
|
||||
|
||||
// Add some text
|
||||
@ -51,13 +56,26 @@ int main(int argc, char *argv[])
|
||||
message.addPart(&attachment);
|
||||
|
||||
// Add an another attachment
|
||||
message.addPart(new MimeAttachment(new QFile("document.pdf")));
|
||||
MimeAttachment document(new QFile("document.pdf"));
|
||||
message.addPart(&document);
|
||||
|
||||
// Now we can send the mail
|
||||
|
||||
smtp.connectToHost();
|
||||
smtp.login();
|
||||
smtp.sendMail(message);
|
||||
if (!smtp.connectToHost()) {
|
||||
qDebug() << "Failed to connect to host!" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!smtp.login()) {
|
||||
qDebug() << "Failed to login!" << endl;
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (!smtp.sendMail(message)) {
|
||||
qDebug() << "Failed to send mail!" << endl;
|
||||
return -3;
|
||||
}
|
||||
|
||||
smtp.quit();
|
||||
|
||||
}
|
28
demos/demo3/demo3.pro
Normal file
28
demos/demo3/demo3.pro
Normal file
@ -0,0 +1,28 @@
|
||||
#-------------------------------------------------
|
||||
#
|
||||
# Project created by QtCreator 2014-10-30T22:20:42
|
||||
#
|
||||
#-------------------------------------------------
|
||||
|
||||
QT += core
|
||||
|
||||
QT -= gui
|
||||
|
||||
TARGET = demo3
|
||||
CONFIG += console
|
||||
CONFIG -= app_bundle
|
||||
|
||||
TEMPLATE = app
|
||||
|
||||
SOURCES += \
|
||||
demo3.cpp
|
||||
|
||||
# Location of SMTP Library
|
||||
SMTP_LIBRARY_LOCATION = $$PWD/../../../build/SMTPEmail-Desktop-Debug
|
||||
|
||||
win32:CONFIG(release, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/release/ -lSMTPEmail
|
||||
else:win32:CONFIG(debug, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/debug/ -lSMTPEmail
|
||||
else:unix: LIBS += -L$$SMTP_LIBRARY_LOCATION -lSMTPEmail
|
||||
|
||||
INCLUDEPATH += $$SMTP_LIBRARY_LOCATION
|
||||
DEPENDPATH += $$SMTP_LIBRARY_LOCATION
|
@ -14,28 +14,32 @@
|
||||
See the LICENSE file for more details.
|
||||
*/
|
||||
|
||||
#include <QtGui/QApplication>
|
||||
#include "../src/SmtpMime"
|
||||
#include <QtCore>
|
||||
|
||||
#include "../../src/SmtpMime"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
QCoreApplication a(argc, argv);
|
||||
|
||||
// First create the SmtpClient object and set the user and the password.
|
||||
|
||||
SmtpClient smtp("smtp.gmail.com", 465, SmtpClient::SslConnection);
|
||||
|
||||
smtp.setUser("your_email@gmail.com");
|
||||
smtp.setUser("your_email@host.com");
|
||||
smtp.setPassword("your_password");
|
||||
|
||||
// Create a MimeMessage
|
||||
|
||||
MimeMessage message;
|
||||
|
||||
message.setSender(new EmailAddress("your_email@gmail.com", "Your Name"));
|
||||
message.addRecipient(new EmailAddress("recipient@host.com", "Recipient's Name"));
|
||||
message.setSubject("SmtpClient for Qt - Example 3 - Html email with images");
|
||||
EmailAddress sender("your_email_address@host.com", "Your Name");
|
||||
message.setSender(&sender);
|
||||
|
||||
EmailAddress to("recipient@host.com", "Recipient's Name");
|
||||
message.addRecipient(&to);
|
||||
|
||||
message.setSubject("SmtpClient for Qt - Example 3 - Html email with images");
|
||||
|
||||
// Now we need to create a MimeHtml object for HTML content
|
||||
MimeHtml html;
|
||||
@ -63,10 +67,21 @@ int main(int argc, char *argv[])
|
||||
message.addPart(&image2);
|
||||
|
||||
// Now the email can be sended
|
||||
if (!smtp.connectToHost()) {
|
||||
qDebug() << "Failed to connect to host!" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!smtp.login()) {
|
||||
qDebug() << "Failed to login!" << endl;
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (!smtp.sendMail(message)) {
|
||||
qDebug() << "Failed to send mail!" << endl;
|
||||
return -3;
|
||||
}
|
||||
|
||||
smtp.connectToHost();
|
||||
smtp.login();
|
||||
smtp.sendMail(message);
|
||||
smtp.quit();
|
||||
|
||||
}
|
28
demos/demo4/demo4.pro
Normal file
28
demos/demo4/demo4.pro
Normal file
@ -0,0 +1,28 @@
|
||||
#-------------------------------------------------
|
||||
#
|
||||
# Project created by QtCreator 2014-10-30T22:20:54
|
||||
#
|
||||
#-------------------------------------------------
|
||||
|
||||
QT += core
|
||||
|
||||
QT -= gui
|
||||
|
||||
TARGET = demo4
|
||||
CONFIG += console
|
||||
CONFIG -= app_bundle
|
||||
|
||||
TEMPLATE = app
|
||||
|
||||
SOURCES += \
|
||||
demo4.cpp
|
||||
|
||||
# Location of SMTP Library
|
||||
SMTP_LIBRARY_LOCATION = $$PWD/../../../build/SMTPEmail-Desktop-Debug
|
||||
|
||||
win32:CONFIG(release, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/release/ -lSMTPEmail
|
||||
else:win32:CONFIG(debug, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/debug/ -lSMTPEmail
|
||||
else:unix: LIBS += -L$$SMTP_LIBRARY_LOCATION -lSMTPEmail
|
||||
|
||||
INCLUDEPATH += $$SMTP_LIBRARY_LOCATION
|
||||
DEPENDPATH += $$SMTP_LIBRARY_LOCATION
|
@ -21,7 +21,9 @@
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class EmailAddress : public QObject
|
||||
#include "smtpexports.h"
|
||||
|
||||
class SMTP_EXPORT EmailAddress : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
@ -24,6 +24,10 @@
|
||||
MimeAttachment::MimeAttachment(QFile *file)
|
||||
: MimeFile(file)
|
||||
{
|
||||
}
|
||||
MimeAttachment::MimeAttachment(const QByteArray& stream, const QString& fileName): MimeFile(stream, fileName)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MimeAttachment::~MimeAttachment()
|
||||
|
@ -23,7 +23,9 @@
|
||||
#include "mimepart.h"
|
||||
#include "mimefile.h"
|
||||
|
||||
class MimeAttachment : public MimeFile
|
||||
#include "smtpexports.h"
|
||||
|
||||
class SMTP_EXPORT MimeAttachment : public MimeFile
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
@ -31,6 +33,8 @@ public:
|
||||
/* [1] Constructors and Destructors */
|
||||
|
||||
MimeAttachment(QFile* file);
|
||||
MimeAttachment(const QByteArray& stream, const QString& fileName);
|
||||
|
||||
~MimeAttachment();
|
||||
|
||||
/* [1] --- */
|
||||
|
@ -22,7 +22,9 @@
|
||||
#include <QObject>
|
||||
#include <QByteArray>
|
||||
|
||||
class MimeContentFormatter : public QObject
|
||||
#include "smtpexports.h"
|
||||
|
||||
class SMTP_EXPORT MimeContentFormatter : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
@ -29,8 +29,18 @@ MimeFile::MimeFile(QFile *file)
|
||||
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 +56,12 @@ MimeFile::~MimeFile()
|
||||
|
||||
void MimeFile::prepare()
|
||||
{
|
||||
if (this->file)
|
||||
{
|
||||
file->open(QIODevice::ReadOnly);
|
||||
this->content = file->readAll();
|
||||
file->close();
|
||||
|
||||
}
|
||||
/* !!! IMPORTANT !!!! */
|
||||
MimePart::prepare();
|
||||
}
|
||||
|
@ -22,13 +22,16 @@
|
||||
#include "mimepart.h"
|
||||
#include <QFile>
|
||||
|
||||
class MimeFile : public MimePart
|
||||
#include "smtpexports.h"
|
||||
|
||||
class SMTP_EXPORT MimeFile : public MimePart
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
/* [1] Constructors and Destructors */
|
||||
|
||||
MimeFile(const QByteArray& stream, const QString& fileName);
|
||||
MimeFile(QFile *f);
|
||||
~MimeFile();
|
||||
|
||||
|
@ -21,7 +21,9 @@
|
||||
|
||||
#include "mimetext.h"
|
||||
|
||||
class MimeHtml : public MimeText
|
||||
#include "smtpexports.h"
|
||||
|
||||
class SMTP_EXPORT MimeHtml : public MimeText
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
@ -21,7 +21,9 @@
|
||||
|
||||
#include "mimefile.h"
|
||||
|
||||
class MimeInlineFile : public MimeFile
|
||||
#include "smtpexports.h"
|
||||
|
||||
class SMTP_EXPORT MimeInlineFile : public MimeFile
|
||||
{
|
||||
public:
|
||||
|
||||
|
@ -19,20 +19,28 @@
|
||||
#include "mimemessage.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QLocale>
|
||||
#include "quotedprintable.h"
|
||||
#include <typeinfo>
|
||||
|
||||
/* [1] Constructors and Destructors */
|
||||
|
||||
MimeMessage::MimeMessage(bool createAutoMimeContent) :
|
||||
replyTo(nullptr),
|
||||
hEncoding(MimePart::_8Bit)
|
||||
{
|
||||
if (createAutoMimeContent)
|
||||
this->content = new MimeMultiPart();
|
||||
|
||||
autoMimeContentCreated = createAutoMimeContent;
|
||||
}
|
||||
|
||||
MimeMessage::~MimeMessage()
|
||||
{
|
||||
if (this->autoMimeContentCreated)
|
||||
{
|
||||
this->autoMimeContentCreated = false;
|
||||
delete (this->content);
|
||||
}
|
||||
}
|
||||
|
||||
/* [1] --- */
|
||||
@ -44,12 +52,22 @@ MimePart& MimeMessage::getContent() {
|
||||
}
|
||||
|
||||
void MimeMessage::setContent(MimePart *content) {
|
||||
if (this->autoMimeContentCreated)
|
||||
{
|
||||
this->autoMimeContentCreated = false;
|
||||
delete (this->content);
|
||||
}
|
||||
this->content = content;
|
||||
}
|
||||
|
||||
void MimeMessage::setReplyTo(EmailAddress* rto) {
|
||||
replyTo = rto;
|
||||
}
|
||||
|
||||
void MimeMessage::setSender(EmailAddress* e)
|
||||
{
|
||||
this->sender = e;
|
||||
e->setParent(this);
|
||||
}
|
||||
|
||||
void MimeMessage::addRecipient(EmailAddress* rcpt, RecipientType type)
|
||||
@ -66,6 +84,8 @@ void MimeMessage::addRecipient(EmailAddress* rcpt, RecipientType type)
|
||||
recipientsBcc << rcpt;
|
||||
break;
|
||||
}
|
||||
|
||||
rcpt->setParent(this);
|
||||
}
|
||||
|
||||
void MimeMessage::addTo(EmailAddress* rcpt) {
|
||||
@ -92,6 +112,11 @@ void MimeMessage::addPart(MimePart *part)
|
||||
};
|
||||
}
|
||||
|
||||
void MimeMessage::setInReplyTo(const QString& inReplyTo)
|
||||
{
|
||||
mInReplyTo = inReplyTo;
|
||||
}
|
||||
|
||||
void MimeMessage::setHeaderEncoding(MimePart::Encoding hEnc)
|
||||
{
|
||||
this->hEncoding = hEnc;
|
||||
@ -116,6 +141,10 @@ const QList<EmailAddress*> & MimeMessage::getRecipients(RecipientType type) cons
|
||||
}
|
||||
}
|
||||
|
||||
const EmailAddress* MimeMessage::getReplyTo() const {
|
||||
return replyTo;
|
||||
}
|
||||
|
||||
const QString & MimeMessage::getSubject() const
|
||||
{
|
||||
return subject;
|
||||
@ -164,7 +193,7 @@ QString MimeMessage::toString()
|
||||
/* ---------------------------------- */
|
||||
|
||||
|
||||
/* ------- Recipients / To ---------- */
|
||||
/* ------- Recipients / To ---------- */
|
||||
mime += "To:";
|
||||
QList<EmailAddress*>::iterator it; int i;
|
||||
for (i = 0, it = recipientsTo.begin(); it != recipientsTo.end(); ++it, ++i)
|
||||
@ -234,10 +263,50 @@ QString MimeMessage::toString()
|
||||
default:
|
||||
mime += subject;
|
||||
}
|
||||
mime += "\r\n";
|
||||
/* ---------------------------------- */
|
||||
|
||||
/* ---------- Reply-To -------------- */
|
||||
if (replyTo) {
|
||||
mime += "Reply-To: ";
|
||||
if (replyTo->getName() != "")
|
||||
{
|
||||
switch (hEncoding)
|
||||
{
|
||||
case MimePart::Base64:
|
||||
mime += " =?utf-8?B?" + QByteArray().append(replyTo->getName()).toBase64() + "?=";
|
||||
break;
|
||||
case MimePart::QuotedPrintable:
|
||||
mime += " =?utf-8?Q?" + QuotedPrintable::encode(QByteArray().append(replyTo->getName())).replace(' ', "_").replace(':',"=3A") + "?=";
|
||||
break;
|
||||
default:
|
||||
mime += " " + replyTo->getName();
|
||||
}
|
||||
}
|
||||
mime += " <" + replyTo->getAddress() + ">\r\n";
|
||||
}
|
||||
|
||||
/* ---------------------------------- */
|
||||
|
||||
mime += "\r\n";
|
||||
mime += "MIME-Version: 1.0\r\n";
|
||||
if (!mInReplyTo.isEmpty())
|
||||
{
|
||||
mime += "In-Reply-To: <" + mInReplyTo + ">\r\n";
|
||||
mime += "References: <" + mInReplyTo + ">\r\n";
|
||||
}
|
||||
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
#if QT_VERSION_MAJOR < 5 //Qt4 workaround since RFC2822Date isn't defined
|
||||
QString shortDayName = QLocale::c().dayName(now.date().dayOfWeek(), QLocale::ShortFormat);
|
||||
QString shortMonthName = QLocale::c().monthName(now.date().month(), QLocale::ShortFormat);
|
||||
int utcOffset = now.secsTo(QDateTime(now.date(), now.time(), Qt::UTC)) / 60;
|
||||
char timezoneSign = utcOffset >= 0 ? '+' : '-';
|
||||
utcOffset = utcOffset >= 0 ? utcOffset : -utcOffset;
|
||||
QString timezone = QString("%1%2%3").arg(timezoneSign).arg(utcOffset / 60, 2, 10, QChar('0')).arg(utcOffset % 60, 2, 10, QChar('0'));
|
||||
mime += QString("Date: %1\r\n").arg(now.toString("%1, dd %2 yyyy hh:mm:ss %3").arg(shortDayName).arg(shortMonthName).arg(timezone));
|
||||
#else //Qt5 supported
|
||||
mime += QString("Date: %1\r\n").arg(now.toString(Qt::RFC2822Date));
|
||||
#endif //support RFC2822Date
|
||||
|
||||
mime += content->toString();
|
||||
return mime;
|
||||
|
@ -24,7 +24,9 @@
|
||||
#include "emailaddress.h"
|
||||
#include <QList>
|
||||
|
||||
class MimeMessage : public QObject
|
||||
#include "smtpexports.h"
|
||||
|
||||
class SMTP_EXPORT MimeMessage : public QObject
|
||||
{
|
||||
public:
|
||||
|
||||
@ -51,6 +53,9 @@ public:
|
||||
void addBcc(EmailAddress* rcpt);
|
||||
void setSubject(const QString & subject);
|
||||
void addPart(MimePart* part);
|
||||
void setReplyTo(EmailAddress* rto);
|
||||
|
||||
void setInReplyTo(const QString& inReplyTo);
|
||||
|
||||
void setHeaderEncoding(MimePart::Encoding);
|
||||
|
||||
@ -58,6 +63,7 @@ public:
|
||||
const QList<EmailAddress*> & getRecipients(RecipientType type = To) const;
|
||||
const QString & getSubject() const;
|
||||
const QList<MimePart*> & getParts() const;
|
||||
const EmailAddress* getReplyTo() const;
|
||||
|
||||
MimePart& getContent();
|
||||
void setContent(MimePart *content);
|
||||
@ -75,9 +81,12 @@ protected:
|
||||
/* [4] Protected members */
|
||||
|
||||
EmailAddress* sender;
|
||||
EmailAddress* replyTo;
|
||||
QList<EmailAddress*> recipientsTo, recipientsCc, recipientsBcc;
|
||||
QString subject;
|
||||
QString mInReplyTo;
|
||||
MimePart *content;
|
||||
bool autoMimeContentCreated;
|
||||
|
||||
MimePart::Encoding hEncoding;
|
||||
|
||||
|
@ -21,7 +21,9 @@
|
||||
|
||||
#include "mimepart.h"
|
||||
|
||||
class MimeMultiPart : public MimePart
|
||||
#include "smtpexports.h"
|
||||
|
||||
class SMTP_EXPORT MimeMultiPart : public MimePart
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
@ -193,7 +193,7 @@ void MimePart::prepare()
|
||||
switch (cEncoding)
|
||||
{
|
||||
case _7Bit:
|
||||
mimeString.append(QString(content).toAscii());
|
||||
mimeString.append(QString(content).toLatin1());
|
||||
break;
|
||||
case _8Bit:
|
||||
mimeString.append(content);
|
||||
|
@ -22,7 +22,9 @@
|
||||
#include <QObject>
|
||||
#include "mimecontentformatter.h"
|
||||
|
||||
class MimePart : public QObject
|
||||
#include "smtpexports.h"
|
||||
|
||||
class SMTP_EXPORT MimePart : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
@ -21,7 +21,9 @@
|
||||
|
||||
#include "mimepart.h"
|
||||
|
||||
class MimeText : public MimePart
|
||||
#include "smtpexports.h"
|
||||
|
||||
class SMTP_EXPORT MimeText : public MimePart
|
||||
{
|
||||
public:
|
||||
|
||||
|
@ -18,9 +18,9 @@
|
||||
|
||||
#include "quotedprintable.h"
|
||||
|
||||
QString& QuotedPrintable::encode(const QByteArray &input)
|
||||
QString QuotedPrintable::encode(const QByteArray &input)
|
||||
{
|
||||
QString *output = new QString();
|
||||
QString output;
|
||||
|
||||
char byte;
|
||||
const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
@ -29,40 +29,41 @@ QString& QuotedPrintable::encode(const QByteArray &input)
|
||||
{
|
||||
byte = input[i];
|
||||
|
||||
if ((byte == 0x20) || (byte >= 33) && (byte <= 126) && (byte != 61))
|
||||
if ((byte == 0x20) || ((byte >= 33) && (byte <= 126) && (byte != 61)))
|
||||
{
|
||||
output->append(byte);
|
||||
output.append(byte);
|
||||
}
|
||||
else
|
||||
{
|
||||
output->append('=');
|
||||
output->append(hex[((byte >> 4) & 0x0F)]);
|
||||
output->append(hex[(byte & 0x0F)]);
|
||||
output.append('=');
|
||||
output.append(hex[((byte >> 4) & 0x0F)]);
|
||||
output.append(hex[(byte & 0x0F)]);
|
||||
}
|
||||
}
|
||||
|
||||
return *output;
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
QByteArray& QuotedPrintable::decode(const QString &input)
|
||||
QByteArray QuotedPrintable::decode(const QString &input)
|
||||
{
|
||||
// 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F
|
||||
const int hexVal[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15};
|
||||
|
||||
QByteArray *output = new QByteArray();
|
||||
QByteArray output;
|
||||
|
||||
for (int i = 0; i < input.length(); ++i)
|
||||
{
|
||||
if (input.at(i).toAscii() == '=')
|
||||
if (input.at(i).toLatin1() == '=')
|
||||
{
|
||||
output->append((hexVal[input.at(++i).toAscii() - '0'] << 4) + hexVal[input.at(++i).toAscii() - '0']);
|
||||
output.append((hexVal[input.at(i + 1).toLatin1() - '0'] << 4) + hexVal[input.at(i + 2).toLatin1() - '0']);
|
||||
i += 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
output->append(input.at(i).toAscii());
|
||||
output.append(input.at(i).toLatin1());
|
||||
}
|
||||
}
|
||||
|
||||
return *output;
|
||||
return output;
|
||||
}
|
||||
|
@ -22,13 +22,15 @@
|
||||
#include <QObject>
|
||||
#include <QByteArray>
|
||||
|
||||
class QuotedPrintable : public QObject
|
||||
#include "smtpexports.h"
|
||||
|
||||
class SMTP_EXPORT QuotedPrintable : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
static QString& encode(const QByteArray &input);
|
||||
static QByteArray& decode(const QString &input);
|
||||
static QString encode(const QByteArray &input);
|
||||
static QByteArray decode(const QString &input);
|
||||
|
||||
private:
|
||||
QuotedPrintable();
|
||||
|
@ -25,10 +25,12 @@
|
||||
/* [1] Constructors and destructors */
|
||||
|
||||
SmtpClient::SmtpClient(const QString & host, int port, ConnectionType connectionType) :
|
||||
socket(NULL),
|
||||
name("localhost"),
|
||||
authMethod(AuthPlain),
|
||||
connectionTimeout(5000),
|
||||
responseTimeout(5000)
|
||||
responseTimeout(5000),
|
||||
sendMessageTimeout(60000)
|
||||
{
|
||||
setConnectionType(connectionType);
|
||||
|
||||
@ -43,7 +45,10 @@ SmtpClient::SmtpClient(const QString & host, int port, ConnectionType connection
|
||||
this, SLOT(socketReadyRead()));
|
||||
}
|
||||
|
||||
SmtpClient::~SmtpClient() {}
|
||||
SmtpClient::~SmtpClient() {
|
||||
if (socket)
|
||||
delete socket;
|
||||
}
|
||||
|
||||
/* [1] --- */
|
||||
|
||||
@ -65,7 +70,7 @@ void SmtpClient::setAuthMethod(AuthMethod method)
|
||||
this->authMethod = method;
|
||||
}
|
||||
|
||||
void SmtpClient::setHost(QString &host)
|
||||
void SmtpClient::setHost(const QString &host)
|
||||
{
|
||||
this->host = host;
|
||||
}
|
||||
@ -79,6 +84,9 @@ void SmtpClient::setConnectionType(ConnectionType ct)
|
||||
{
|
||||
this->connectionType = ct;
|
||||
|
||||
if (socket)
|
||||
delete socket;
|
||||
|
||||
switch (connectionType)
|
||||
{
|
||||
case TcpConnection:
|
||||
@ -144,6 +152,34 @@ QTcpSocket* SmtpClient::getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
int SmtpClient::getConnectionTimeout() const
|
||||
{
|
||||
return connectionTimeout;
|
||||
}
|
||||
|
||||
void SmtpClient::setConnectionTimeout(int msec)
|
||||
{
|
||||
connectionTimeout = msec;
|
||||
}
|
||||
|
||||
int SmtpClient::getResponseTimeout() const
|
||||
{
|
||||
return responseTimeout;
|
||||
}
|
||||
|
||||
void SmtpClient::setResponseTimeout(int msec)
|
||||
{
|
||||
responseTimeout = msec;
|
||||
}
|
||||
int SmtpClient::getSendMessageTimeout() const
|
||||
{
|
||||
return sendMessageTimeout;
|
||||
}
|
||||
void SmtpClient::setSendMessageTimeout(int msec)
|
||||
{
|
||||
sendMessageTimeout = msec;
|
||||
}
|
||||
|
||||
/* [2] --- */
|
||||
|
||||
|
||||
@ -213,7 +249,7 @@ bool SmtpClient::connectToHost()
|
||||
|
||||
if (!((QSslSocket*) socket)->waitForEncrypted(connectionTimeout)) {
|
||||
qDebug() << ((QSslSocket*) socket)->errorString();
|
||||
emit SmtpError(ConnectionTimeoutError);
|
||||
emit smtpError(ConnectionTimeoutError);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -234,6 +270,10 @@ bool SmtpClient::connectToHost()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (SendMessageTimeoutException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// If no errors occured the function returns true.
|
||||
return true;
|
||||
@ -292,12 +332,18 @@ bool SmtpClient::login(const QString &user, const QString &password, AuthMethod
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ResponseTimeoutException e)
|
||||
catch (ResponseTimeoutException)
|
||||
{
|
||||
// Responce Timeout exceeded
|
||||
emit smtpError(AuthenticationFailedError);
|
||||
return false;
|
||||
}
|
||||
catch (SendMessageTimeoutException)
|
||||
{
|
||||
// Send Timeout exceeded
|
||||
emit smtpError(AuthenticationFailedError);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -307,7 +353,7 @@ bool SmtpClient::sendMail(MimeMessage& email)
|
||||
try
|
||||
{
|
||||
// Send the MAIL command with the sender
|
||||
sendMessage("MAIL FROM: <" + email.getSender().getAddress() + ">");
|
||||
sendMessage("MAIL FROM:<" + email.getSender().getAddress() + ">");
|
||||
|
||||
waitForResponse();
|
||||
|
||||
@ -319,7 +365,8 @@ bool SmtpClient::sendMail(MimeMessage& email)
|
||||
for (it = email.getRecipients().begin(), itEnd = email.getRecipients().end();
|
||||
it != itEnd; ++it)
|
||||
{
|
||||
sendMessage("RCPT TO: <" + (*it)->getAddress() + ">");
|
||||
|
||||
sendMessage("RCPT TO:<" + (*it)->getAddress() + ">");
|
||||
waitForResponse();
|
||||
|
||||
if (responseCode != 250) return false;
|
||||
@ -329,7 +376,7 @@ bool SmtpClient::sendMail(MimeMessage& email)
|
||||
for (it = email.getRecipients(MimeMessage::Cc).begin(), itEnd = email.getRecipients(MimeMessage::Cc).end();
|
||||
it != itEnd; ++it)
|
||||
{
|
||||
sendMessage("RCPT TO: <" + (*it)->getAddress() + ">");
|
||||
sendMessage("RCPT TO:<" + (*it)->getAddress() + ">");
|
||||
waitForResponse();
|
||||
|
||||
if (responseCode != 250) return false;
|
||||
@ -339,7 +386,7 @@ bool SmtpClient::sendMail(MimeMessage& email)
|
||||
for (it = email.getRecipients(MimeMessage::Bcc).begin(), itEnd = email.getRecipients(MimeMessage::Bcc).end();
|
||||
it != itEnd; ++it)
|
||||
{
|
||||
sendMessage("RCPT TO: <" + (*it)->getAddress() + ">");
|
||||
sendMessage("RCPT TO:<" + (*it)->getAddress() + ">");
|
||||
waitForResponse();
|
||||
|
||||
if (responseCode != 250) return false;
|
||||
@ -364,13 +411,26 @@ bool SmtpClient::sendMail(MimeMessage& email)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (SendMessageTimeoutException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SmtpClient::quit()
|
||||
{
|
||||
sendMessage("QUIT");
|
||||
try
|
||||
{
|
||||
sendMessage("QUIT");
|
||||
}
|
||||
catch(SmtpClient::SendMessageTimeoutException)
|
||||
{
|
||||
//Manually close the connection to the smtp server if message "QUIT" wasn't received by the smtp server
|
||||
if(socket->state() == QAbstractSocket::ConnectedState || socket->state() == QAbstractSocket::ConnectingState || socket->state() == QAbstractSocket::HostLookupState)
|
||||
socket->disconnectFromHost();
|
||||
}
|
||||
}
|
||||
|
||||
/* [3] --- */
|
||||
@ -378,30 +438,41 @@ void SmtpClient::quit()
|
||||
|
||||
/* [4] Protected methods */
|
||||
|
||||
void SmtpClient::waitForResponse() throw (ResponseTimeoutException)
|
||||
void SmtpClient::waitForResponse()
|
||||
{
|
||||
if (!socket->waitForReadyRead(responseTimeout))
|
||||
{
|
||||
emit smtpError(ResponseTimeoutError);
|
||||
throw ResponseTimeoutException();
|
||||
}
|
||||
do {
|
||||
if (!socket->waitForReadyRead(responseTimeout))
|
||||
{
|
||||
emit smtpError(ResponseTimeoutError);
|
||||
throw ResponseTimeoutException();
|
||||
}
|
||||
|
||||
// Save the server's response
|
||||
responseText = socket->readAll();
|
||||
while (socket->canReadLine()) {
|
||||
// Save the server's response
|
||||
responseText = socket->readLine();
|
||||
|
||||
// Extract the respose code from the server's responce (first 3 digits)
|
||||
responseCode = responseText.left(3).toInt();
|
||||
// Extract the respose code from the server's responce (first 3 digits)
|
||||
responseCode = responseText.left(3).toInt();
|
||||
|
||||
if (responseCode / 100 == 4)
|
||||
emit smtpError(ServerError);
|
||||
if (responseCode / 100 == 4)
|
||||
emit smtpError(ServerError);
|
||||
|
||||
if (responseCode / 100 == 5)
|
||||
emit smtpError(ClientError);
|
||||
if (responseCode / 100 == 5)
|
||||
emit smtpError(ClientError);
|
||||
|
||||
if (responseText[3] == ' ') { return; }
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
|
||||
void SmtpClient::sendMessage(const QString &text)
|
||||
{
|
||||
socket->write(text.toUtf8() + "\r\n");
|
||||
if (! socket->waitForBytesWritten(sendMessageTimeout))
|
||||
{
|
||||
emit smtpError(SendDataTimeoutError);
|
||||
throw SendMessageTimeoutException();
|
||||
}
|
||||
}
|
||||
|
||||
/* [4] --- */
|
||||
@ -409,11 +480,11 @@ void SmtpClient::sendMessage(const QString &text)
|
||||
|
||||
/* [5] Slots for the socket's signals */
|
||||
|
||||
void SmtpClient::socketStateChanged(QAbstractSocket::SocketState state)
|
||||
void SmtpClient::socketStateChanged(QAbstractSocket::SocketState /*state*/)
|
||||
{
|
||||
}
|
||||
|
||||
void SmtpClient::socketError(QAbstractSocket::SocketError socketError)
|
||||
void SmtpClient::socketError(QAbstractSocket::SocketError /*socketError*/)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -23,9 +23,9 @@
|
||||
#include <QtNetwork/QSslSocket>
|
||||
|
||||
#include "mimemessage.h"
|
||||
#include "smtpexports.h"
|
||||
|
||||
|
||||
class SmtpClient : public QObject
|
||||
class SMTP_EXPORT SmtpClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
@ -42,6 +42,7 @@ public:
|
||||
{
|
||||
ConnectionTimeoutError,
|
||||
ResponseTimeoutError,
|
||||
SendDataTimeoutError,
|
||||
AuthenticationFailedError,
|
||||
ServerError, // 4xx smtp error
|
||||
ClientError // 5xx smtp error
|
||||
@ -59,7 +60,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();
|
||||
|
||||
@ -69,7 +70,7 @@ public:
|
||||
/* [2] Getters and Setters */
|
||||
|
||||
const QString& getHost() const;
|
||||
void setHost(QString &host);
|
||||
void setHost(const QString &host);
|
||||
|
||||
int getPort() const;
|
||||
void setPort(int port);
|
||||
@ -81,7 +82,7 @@ public:
|
||||
void setConnectionType(ConnectionType ct);
|
||||
|
||||
const QString & getUser() const;
|
||||
void setUser(const QString &host);
|
||||
void setUser(const QString &user);
|
||||
|
||||
const QString & getPassword() const;
|
||||
void setPassword(const QString &password);
|
||||
@ -92,6 +93,15 @@ public:
|
||||
const QString & getResponseText() const;
|
||||
int getResponseCode() const;
|
||||
|
||||
int getConnectionTimeout() const;
|
||||
void setConnectionTimeout(int msec);
|
||||
|
||||
int getResponseTimeout() const;
|
||||
void setResponseTimeout(int msec);
|
||||
|
||||
int getSendMessageTimeout() const;
|
||||
void setSendMessageTimeout(int msec);
|
||||
|
||||
QTcpSocket* getSocket();
|
||||
|
||||
|
||||
@ -129,19 +139,22 @@ protected:
|
||||
|
||||
int connectionTimeout;
|
||||
int responseTimeout;
|
||||
|
||||
int sendMessageTimeout;
|
||||
|
||||
|
||||
QString responseText;
|
||||
int responseCode;
|
||||
|
||||
|
||||
class ResponseTimeoutException {};
|
||||
class SendMessageTimeoutException {};
|
||||
|
||||
/* [4] --- */
|
||||
|
||||
|
||||
/* [5] Protected methods */
|
||||
|
||||
void waitForResponse() throw (ResponseTimeoutException);
|
||||
void waitForResponse();
|
||||
|
||||
void sendMessage(const QString &text);
|
||||
|
||||
@ -162,7 +175,7 @@ signals:
|
||||
|
||||
/* [7] Signals */
|
||||
|
||||
void smtpError(SmtpError e);
|
||||
void smtpError(SmtpClient::SmtpError e);
|
||||
|
||||
/* [7] --- */
|
||||
|
||||
|
12
src/smtpexports.h
Normal file
12
src/smtpexports.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef SMTPEXPORTS_H
|
||||
#define SMTPEXPORTS_H
|
||||
|
||||
#ifdef SMTP_BUILD
|
||||
#define SMTP_EXPORT Q_DECL_EXPORT
|
||||
#elseif SMTP_USE
|
||||
#define SMTP_EXPORT Q_DECL_IMPORT
|
||||
#else
|
||||
#define SMTP_EXPORT
|
||||
#endif
|
||||
|
||||
#endif // SMTPEXPORTS_H
|
Loading…
Reference in New Issue
Block a user