master
feiyangqingyun 2019-11-26 10:01:27 +08:00
parent 1d371bef97
commit 617764b181
4 changed files with 77 additions and 74 deletions

View File

@ -31,7 +31,7 @@ void frmEmailTool::on_btnSend_clicked()
return; return;
} }
SendEmailThread::Instance()->setEmialTitle(ui->txtTitle->text()); SendEmailThread::Instance()->setEmailTitle(ui->txtTitle->text());
SendEmailThread::Instance()->setSendEmailAddr(ui->txtSenderAddr->text()); SendEmailThread::Instance()->setSendEmailAddr(ui->txtSenderAddr->text());
SendEmailThread::Instance()->setSendEmailPwd(ui->txtSenderPwd->text()); SendEmailThread::Instance()->setSendEmailPwd(ui->txtSenderPwd->text());
SendEmailThread::Instance()->setReceiveEmailAddr(ui->txtReceiverAddr->text()); SendEmailThread::Instance()->setReceiveEmailAddr(ui->txtReceiverAddr->text());

View File

@ -1,6 +1,9 @@
#include "sendemailthread.h" #include "sendemailthread.h"
#include "sendemail/smtpmime.h" #include "sendemail/smtpmime.h"
#pragma execution_character_set("utf-8")
#define TIMEMS qPrintable(QTime::currentTime().toString("hh:mm:ss zzz"))
QScopedPointer<SendEmailThread> SendEmailThread::self; QScopedPointer<SendEmailThread> SendEmailThread::self;
SendEmailThread *SendEmailThread::Instance() SendEmailThread *SendEmailThread::Instance()
{ {
@ -17,12 +20,12 @@ SendEmailThread *SendEmailThread::Instance()
SendEmailThread::SendEmailThread(QObject *parent) : QThread(parent) SendEmailThread::SendEmailThread(QObject *parent) : QThread(parent)
{ {
stopped = false; stopped = false;
emialTitle = "邮件标题"; emialTitle = "邮件标题";
sendEmailAddr = "feiyangqingyun@126.com"; sendEmailAddr = "feiyangqingyun@126.com";
sendEmailPwd = "123456789"; sendEmailPwd = "123456789";
receiveEmailAddr = "feiyangqingyun@163.com;517216493@qq.com"; receiveEmailAddr = "feiyangqingyun@163.com;517216493@qq.com";
contents.clear(); contents.clear();
fileNames.clear(); fileNames.clear();
} }
@ -34,83 +37,83 @@ SendEmailThread::~SendEmailThread()
void SendEmailThread::run() void SendEmailThread::run()
{ {
while (!stopped) { while (!stopped) {
int count = contents.count(); int count = contents.count();
if (count > 0) { if (count > 0) {
mutex.lock(); mutex.lock();
QString content = contents.takeFirst(); QString content = contents.takeFirst();
QString fileName = fileNames.takeFirst(); QString fileName = fileNames.takeFirst();
mutex.unlock(); mutex.unlock();
QString result; QString result;
QStringList list = sendEmailAddr.split("@"); QStringList list = sendEmailAddr.split("@");
QString tempSMTP = list.at(1).split(".").at(0); QString tempSMTP = list.at(1).split(".").at(0);
int tempPort = 25; int tempPort = 25;
//QQ邮箱端口号为465,必须启用SSL协议. //QQ邮箱端口号为465,必须启用SSL协议.
if (tempSMTP.toUpper() == "QQ") { if (tempSMTP.toUpper() == "QQ") {
tempPort = 465; tempPort = 465;
} }
SmtpClient smtp(QString("smtp.%1.com").arg(tempSMTP), tempPort, tempPort == 25 ? SmtpClient::TcpConnection : SmtpClient::SslConnection); SmtpClient smtp(QString("smtp.%1.com").arg(tempSMTP), tempPort, tempPort == 25 ? SmtpClient::TcpConnection : SmtpClient::SslConnection);
smtp.setUser(sendEmailAddr); smtp.setUser(sendEmailAddr);
smtp.setPassword(sendEmailPwd); smtp.setPassword(sendEmailPwd);
//构建邮件主题,包含发件人收件人附件等. //构建邮件主题,包含发件人收件人附件等.
MimeMessage message; MimeMessage message;
message.setSender(new EmailAddress(sendEmailAddr)); message.setSender(new EmailAddress(sendEmailAddr));
//逐个添加收件人 //逐个添加收件人
QStringList receiver = receiveEmailAddr.split(';'); QStringList receiver = receiveEmailAddr.split(';');
for (int i = 0; i < receiver.size(); i++) { for (int i = 0; i < receiver.size(); i++) {
message.addRecipient(new EmailAddress(receiver.at(i))); message.addRecipient(new EmailAddress(receiver.at(i)));
} }
//构建邮件标题 //构建邮件标题
message.setSubject(emialTitle); message.setSubject(emialTitle);
//构建邮件正文 //构建邮件正文
MimeHtml text; MimeHtml text;
text.setHtml(content); text.setHtml(content);
message.addPart(&text); message.addPart(&text);
//构建附件-报警图像 //构建附件-报警图像
if (fileName.length() > 0) { if (fileName.length() > 0) {
QStringList attas = fileName.split(";"); QStringList attas = fileName.split(";");
foreach (QString tempAtta, attas) { foreach (QString tempAtta, attas) {
QFile *file = new QFile(tempAtta); QFile *file = new QFile(tempAtta);
if (file->exists()) { if (file->exists()) {
message.addPart(new MimeAttachment(file)); message.addPart(new MimeAttachment(file));
} }
} }
} }
if (!smtp.connectToHost()) { if (!smtp.connectToHost()) {
result = "邮件服务器连接失败"; result = "邮件服务器连接失败";
} else { } else {
if (!smtp.login()) { if (!smtp.login()) {
result = "邮件用户登录失败"; result = "邮件用户登录失败";
} else { } else {
if (!smtp.sendMail(message)) { if (!smtp.sendMail(message)) {
result = "邮件发送失败"; result = "邮件发送失败";
} else { } else {
result = "邮件发送成功"; result = "邮件发送成功";
} }
} }
} }
smtp.quit(); smtp.quit();
if (!result.isEmpty()) { if (!result.isEmpty()) {
emit receiveEmailResult(result); emit receiveEmailResult(result);
} }
msleep(1000); msleep(1000);
} }
msleep(100); msleep(100);
} }
stopped = false; stopped = false;
} }
void SendEmailThread::stop() void SendEmailThread::stop()
@ -118,30 +121,30 @@ void SendEmailThread::stop()
stopped = true; stopped = true;
} }
void SendEmailThread::setEmialTitle(const QString &emailTitle) void SendEmailThread::setEmailTitle(const QString &emailTitle)
{ {
this->emialTitle = emailTitle; this->emialTitle = emailTitle;
} }
void SendEmailThread::setSendEmailAddr(const QString &sendEmailAddr) void SendEmailThread::setSendEmailAddr(const QString &sendEmailAddr)
{ {
this->sendEmailAddr = sendEmailAddr; this->sendEmailAddr = sendEmailAddr;
} }
void SendEmailThread::setSendEmailPwd(const QString &sendEmailPwd) void SendEmailThread::setSendEmailPwd(const QString &sendEmailPwd)
{ {
this->sendEmailPwd = sendEmailPwd; this->sendEmailPwd = sendEmailPwd;
} }
void SendEmailThread::setReceiveEmailAddr(const QString &receiveEmailAddr) void SendEmailThread::setReceiveEmailAddr(const QString &receiveEmailAddr)
{ {
this->receiveEmailAddr = receiveEmailAddr; this->receiveEmailAddr = receiveEmailAddr;
} }
void SendEmailThread::append(const QString &content, const QString &fileName) void SendEmailThread::append(const QString &content, const QString &fileName)
{ {
mutex.lock(); mutex.lock();
contents.append(content); contents.append(content);
fileNames.append(fileName); fileNames.append(fileName);
mutex.unlock(); mutex.unlock();
} }

View File

@ -33,7 +33,7 @@ signals:
public slots: public slots:
void stop(); void stop();
void setEmialTitle(const QString &emailTitle); void setEmailTitle(const QString &emailTitle);
void setSendEmailAddr(const QString &sendEmailAddr); void setSendEmailAddr(const QString &sendEmailAddr);
void setSendEmailPwd(const QString &sendEmailPwd); void setSendEmailPwd(const QString &sendEmailPwd);
void setReceiveEmailAddr(const QString &receiveEmailAddr); void setReceiveEmailAddr(const QString &receiveEmailAddr);

BIN
email/snap.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB