diff --git a/SMTPEmail.pro b/SMTPEmail.pro index de6f321..c409945 100644 --- a/SMTPEmail.pro +++ b/SMTPEmail.pro @@ -20,7 +20,8 @@ SOURCES += \ src/mimepart.cpp \ src/mimetext.cpp \ src/smtpclient.cpp \ - demos/demo1.cpp + demos/demo2/sendemail.cpp \ + demos/demo2/demo2.cpp HEADERS += \ src/emailaddress.h \ @@ -32,6 +33,10 @@ HEADERS += \ src/mimepart.h \ src/mimetext.h \ src/smtpclient.h \ - src/SmtpMime + src/SmtpMime \ + demos/demo2/sendemail.h OTHER_FILES += + +FORMS += \ + demos/demo2/sendemail.ui diff --git a/demos/demo2/demo2.cpp b/demos/demo2/demo2.cpp new file mode 100644 index 0000000..a11c598 --- /dev/null +++ b/demos/demo2/demo2.cpp @@ -0,0 +1,24 @@ +#include + +#include "sendemail.h" +#include "../../src/SmtpMime" + + +#include + +using namespace std; + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + SendEmail form (NULL); + + form.setWindowTitle("SmtpClient for Qt - Demo 2"); + + form.show(); + + a.exec(); + + +} + diff --git a/demos/demo2/sendemail.cpp b/demos/demo2/sendemail.cpp new file mode 100644 index 0000000..7832282 --- /dev/null +++ b/demos/demo2/sendemail.cpp @@ -0,0 +1,118 @@ +#include "sendemail.h" +#include "ui_sendemail.h" + +#include +#include + +#include + +using namespace std; + +SendEmail::SendEmail(QWidget *parent) : + QWidget(parent), + ui(new Ui::SendEmail) +{ + ui->setupUi(this); +} + +SendEmail::~SendEmail() +{ + delete ui; +} + +EmailAddress* SendEmail::stringToEmail(const QString &str) +{ + int p1 = str.indexOf("<"); + int p2 = str.indexOf(">"); + + if (p1 == -1) + { + // no name, only email address + return new EmailAddress(str); + } + else + { + return new EmailAddress(str.mid(p1 + 1, p2 - p1 - 1), str.left(p1)); + } + +} + +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() +{ + QString host = ui->host->text(); + int port = ui->port->value(); + bool ssl = ui->ssl->isChecked(); + bool auth = ui->auth->isChecked(); + QString user = ui->username->text(); + QString password = ui->password->text(); + + EmailAddress *sender = stringToEmail(ui->sender->text()); + + QStringList rcptStringList = ui->recipients->text().split(';'); + + QString subject = ui->subject->text(); + QString html = ui->texteditor->toHtml(); + + SmtpClient smtp (host, port, ssl ? SmtpClient::SslConnection : SmtpClient::TcpConnection); + + MimeMessage message; + + message.setSender(sender); + message.setSubject(subject); + + for (int i = 0; i < rcptStringList.size(); ++i) + message.addRecipient(stringToEmail(rcptStringList.at(i))); + + MimeHtml content; + content.setHtml(html); + + message.addPart(&content); + + for (int i = 0; i < ui->attachments->count(); ++i) + { + message.addPart(new MimeFile(new QFile(ui->attachments->item(i)->text()))); + } + + if (!smtp.connectToHost()) + { + errorMessage("Connection Failed"); + return; + } + + if (auth) + if (!smtp.login(user, password)) + { + errorMessage("Authentification Failed"); + return; + } + + if (!smtp.sendMail(message)) + { + errorMessage("Mail sending failed"); + return; + } + + smtp.quit(); + +} + +void SendEmail::errorMessage(const QString &message) +{ + QErrorMessage err (this); + + err.showMessage(message); + + err.exec(); +} diff --git a/demos/demo2/sendemail.h b/demos/demo2/sendemail.h new file mode 100644 index 0000000..5e3b15f --- /dev/null +++ b/demos/demo2/sendemail.h @@ -0,0 +1,35 @@ +#ifndef SENDEMAIL_H +#define SENDEMAIL_H + +#include + +#include "../../src/SmtpMime" + + + +namespace Ui { + class SendEmail; +} + +class SendEmail : public QWidget +{ + Q_OBJECT + +public: + explicit SendEmail(QWidget *parent = 0); + ~SendEmail(); + + static EmailAddress * stringToEmail(const QString & str); + +private slots: + void on_addAttachment_clicked(); + + void on_sendEmail_clicked(); + +private: + Ui::SendEmail *ui; + + void errorMessage(const QString & message); +}; + +#endif // SENDEMAIL_H