diff --git a/email/frmemailtool.cpp b/email/frmemailtool.cpp index 502ebe9..195dccc 100644 --- a/email/frmemailtool.cpp +++ b/email/frmemailtool.cpp @@ -31,7 +31,7 @@ void frmEmailTool::on_btnSend_clicked() return; } - SendEmailThread::Instance()->setEmialTitle(ui->txtTitle->text()); + SendEmailThread::Instance()->setEmailTitle(ui->txtTitle->text()); SendEmailThread::Instance()->setSendEmailAddr(ui->txtSenderAddr->text()); SendEmailThread::Instance()->setSendEmailPwd(ui->txtSenderPwd->text()); SendEmailThread::Instance()->setReceiveEmailAddr(ui->txtReceiverAddr->text()); diff --git a/email/sendemail/sendemailthread.cpp b/email/sendemail/sendemailthread.cpp index 82c8468..d9cb259 100644 --- a/email/sendemail/sendemailthread.cpp +++ b/email/sendemail/sendemailthread.cpp @@ -1,6 +1,9 @@ #include "sendemailthread.h" #include "sendemail/smtpmime.h" +#pragma execution_character_set("utf-8") +#define TIMEMS qPrintable(QTime::currentTime().toString("hh:mm:ss zzz")) + QScopedPointer SendEmailThread::self; SendEmailThread *SendEmailThread::Instance() { @@ -17,100 +20,100 @@ SendEmailThread *SendEmailThread::Instance() SendEmailThread::SendEmailThread(QObject *parent) : QThread(parent) { - stopped = false; - emialTitle = "邮件标题"; - sendEmailAddr = "feiyangqingyun@126.com"; - sendEmailPwd = "123456789"; - receiveEmailAddr = "feiyangqingyun@163.com;517216493@qq.com"; - contents.clear(); + stopped = false; + emialTitle = "邮件标题"; + sendEmailAddr = "feiyangqingyun@126.com"; + sendEmailPwd = "123456789"; + receiveEmailAddr = "feiyangqingyun@163.com;517216493@qq.com"; + contents.clear(); fileNames.clear(); } SendEmailThread::~SendEmailThread() { - this->stop(); + this->stop(); this->wait(1000); } void SendEmailThread::run() { - while (!stopped) { - int count = contents.count(); - if (count > 0) { + while (!stopped) { + int count = contents.count(); + if (count > 0) { mutex.lock(); - QString content = contents.takeFirst(); - QString fileName = fileNames.takeFirst(); + QString content = contents.takeFirst(); + QString fileName = fileNames.takeFirst(); mutex.unlock(); - QString result; - QStringList list = sendEmailAddr.split("@"); - QString tempSMTP = list.at(1).split(".").at(0); - int tempPort = 25; + QString result; + QStringList list = sendEmailAddr.split("@"); + QString tempSMTP = list.at(1).split(".").at(0); + int tempPort = 25; - //QQ邮箱端口号为465,必须启用SSL协议. - if (tempSMTP.toUpper() == "QQ") { - tempPort = 465; - } + //QQ邮箱端口号为465,必须启用SSL协议. + if (tempSMTP.toUpper() == "QQ") { + tempPort = 465; + } SmtpClient smtp(QString("smtp.%1.com").arg(tempSMTP), tempPort, tempPort == 25 ? SmtpClient::TcpConnection : SmtpClient::SslConnection); - smtp.setUser(sendEmailAddr); - smtp.setPassword(sendEmailPwd); + smtp.setUser(sendEmailAddr); + smtp.setPassword(sendEmailPwd); - //构建邮件主题,包含发件人收件人附件等. - MimeMessage message; - message.setSender(new EmailAddress(sendEmailAddr)); + //构建邮件主题,包含发件人收件人附件等. + MimeMessage message; + message.setSender(new EmailAddress(sendEmailAddr)); - //逐个添加收件人 - QStringList receiver = receiveEmailAddr.split(';'); - for (int i = 0; i < receiver.size(); i++) { - message.addRecipient(new EmailAddress(receiver.at(i))); - } + //逐个添加收件人 + QStringList receiver = receiveEmailAddr.split(';'); + for (int i = 0; i < receiver.size(); i++) { + message.addRecipient(new EmailAddress(receiver.at(i))); + } - //构建邮件标题 - message.setSubject(emialTitle); + //构建邮件标题 + message.setSubject(emialTitle); - //构建邮件正文 - MimeHtml text; - text.setHtml(content); - message.addPart(&text); + //构建邮件正文 + MimeHtml text; + text.setHtml(content); + message.addPart(&text); //构建附件-报警图像 - 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 (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 = "邮件发送成功"; - } - } - } + 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); - } + smtp.quit(); + if (!result.isEmpty()) { + emit receiveEmailResult(result); + } msleep(1000); - } + } msleep(100); - } + } - stopped = false; + stopped = false; } void SendEmailThread::stop() @@ -118,30 +121,30 @@ void SendEmailThread::stop() 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) { - this->sendEmailAddr = sendEmailAddr; + this->sendEmailAddr = sendEmailAddr; } void SendEmailThread::setSendEmailPwd(const QString &sendEmailPwd) { - this->sendEmailPwd = sendEmailPwd; + this->sendEmailPwd = sendEmailPwd; } void SendEmailThread::setReceiveEmailAddr(const QString &receiveEmailAddr) { - this->receiveEmailAddr = receiveEmailAddr; + this->receiveEmailAddr = receiveEmailAddr; } void SendEmailThread::append(const QString &content, const QString &fileName) { mutex.lock(); - contents.append(content); - fileNames.append(fileName); + contents.append(content); + fileNames.append(fileName); mutex.unlock(); } diff --git a/email/sendemail/sendemailthread.h b/email/sendemail/sendemailthread.h index c4ef347..e02735f 100644 --- a/email/sendemail/sendemailthread.h +++ b/email/sendemail/sendemailthread.h @@ -33,7 +33,7 @@ signals: public slots: void stop(); - void setEmialTitle(const QString &emailTitle); + void setEmailTitle(const QString &emailTitle); void setSendEmailAddr(const QString &sendEmailAddr); void setSendEmailPwd(const QString &sendEmailPwd); void setReceiveEmailAddr(const QString &receiveEmailAddr); diff --git a/email/snap.jpg b/email/snap.jpg new file mode 100644 index 0000000..19ff5b8 Binary files /dev/null and b/email/snap.jpg differ