2011-09-02 12:20:33 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2011 - Tőkés Attila
|
|
|
|
|
|
|
|
This file is part of SmtpClient for Qt.
|
|
|
|
|
|
|
|
SmtpClient for Qt is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
SmtpClient for Qt is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY.
|
|
|
|
|
2011-09-02 15:24:39 +00:00
|
|
|
See the LICENSE file for more details.
|
2011-09-02 12:20:33 +00:00
|
|
|
*/
|
|
|
|
|
2011-08-31 17:07:33 +00:00
|
|
|
#include "sendemail.h"
|
|
|
|
#include "ui_sendemail.h"
|
|
|
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QErrorMessage>
|
2011-11-02 14:17:33 +00:00
|
|
|
#include <QMessageBox>
|
2011-08-31 17:07:33 +00:00
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
2011-09-06 16:08:11 +00:00
|
|
|
message.addPart(new MimeAttachment(new QFile(ui->attachments->item(i)->text())));
|
2011-08-31 17:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2011-11-02 14:17:33 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
QMessageBox okMessage (this);
|
2014-10-30 20:56:39 +00:00
|
|
|
okMessage.setText("The email was succesfully sent.");
|
2011-11-02 14:17:33 +00:00
|
|
|
okMessage.exec();
|
|
|
|
}
|
2011-08-31 17:07:33 +00:00
|
|
|
|
|
|
|
smtp.quit();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendEmail::errorMessage(const QString &message)
|
|
|
|
{
|
|
|
|
QErrorMessage err (this);
|
|
|
|
|
|
|
|
err.showMessage(message);
|
|
|
|
|
|
|
|
err.exec();
|
|
|
|
}
|