Demo 2 Application added to project.

This commit is contained in:
Your Name 2011-08-31 20:07:33 +03:00
parent ff4988dd39
commit 140bb2fa08
4 changed files with 184 additions and 2 deletions

View File

@ -20,7 +20,8 @@ SOURCES += \
src/mimepart.cpp \ src/mimepart.cpp \
src/mimetext.cpp \ src/mimetext.cpp \
src/smtpclient.cpp \ src/smtpclient.cpp \
demos/demo1.cpp demos/demo2/sendemail.cpp \
demos/demo2/demo2.cpp
HEADERS += \ HEADERS += \
src/emailaddress.h \ src/emailaddress.h \
@ -32,6 +33,10 @@ HEADERS += \
src/mimepart.h \ src/mimepart.h \
src/mimetext.h \ src/mimetext.h \
src/smtpclient.h \ src/smtpclient.h \
src/SmtpMime src/SmtpMime \
demos/demo2/sendemail.h
OTHER_FILES += OTHER_FILES +=
FORMS += \
demos/demo2/sendemail.ui

24
demos/demo2/demo2.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <QtGui/QApplication>
#include "sendemail.h"
#include "../../src/SmtpMime"
#include <iostream>
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();
}

118
demos/demo2/sendemail.cpp Normal file
View File

@ -0,0 +1,118 @@
#include "sendemail.h"
#include "ui_sendemail.h"
#include <QFileDialog>
#include <QErrorMessage>
#include <iostream>
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();
}

35
demos/demo2/sendemail.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef SENDEMAIL_H
#define SENDEMAIL_H
#include <QWidget>
#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