qt_demoe/emailtool/sendemail/sendemailthread.cpp

151 lines
4.1 KiB
C++
Raw Normal View History

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