-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathsendemailthread.cpp
More file actions
98 lines (83 loc) · 2.69 KB
/
sendemailthread.cpp
File metadata and controls
98 lines (83 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
#include "sendemailthread.h"
#include "myhelper.h"
#include "api.h"
#include "dbhelper.h"
#include "qemail/smtpmime.h"
SendEmailThread::SendEmailThread(QObject *parent) : QThread(parent)
{
content = "";
headMsg = "端口:邮件端口";
}
void SendEmailThread::SetContent(QString content)
{
this->content = content;
}
void SendEmailThread::SetFileName(QString fileName)
{
this->fileName = fileName;
}
void SendEmailThread::run()
{
QString str;
//发送告警邮件
//实例化发送邮件对象
QStringList list = App::SendEmailAddr.split("@");
if (list.count() < 2) {
str = "发件人邮箱格式错误";
} else {
QString tempSMTP = list.at(1).split(".").at(0);
int tempPort = 25;
//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(App::SendEmailAddr);
smtp.setPassword(App::SendEmailPwd);
//构建邮件主题,包含发件人收件人附件等.
MimeMessage message;
message.setSender(new EmailAddress(App::SendEmailAddr));
//逐个添加收件人
QStringList receiver = App::EmailAddr.split(';');
for (int i = 0; i < receiver.size(); i++) {
message.addRecipient(new EmailAddress(receiver.at(i)));
}
//构建邮件标题
message.setSubject(App::Title + "告警邮件");
//构建邮件正文
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 (!smtp.connectToHost()) {
str = "邮件服务器连接失败";
} else {
if (!smtp.login()) {
str = "邮件用户登录失败";
} else {
if (!smtp.sendMail(message)) {
str = "告警邮件发送失败";
} else {
str = "告警邮件发送成功";
}
}
}
smtp.quit();
}
qDebug() << TIME << headMsg << "命令解析:" << str;
API::AddMessage(str);
DBHelper::AddEventInfoUser(str);
}