-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvdlemail.cpp
174 lines (153 loc) · 5.01 KB
/
vdlemail.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "vdlemail.h"
VDLEmail::VDLEmail()
{
}
void VDLEmail::sendEmail(QString SmtpServer,int SmtpPort,QString SenderName,
QString SenderEmail,QString SenderPassword,QString ReceiveName,
QString ReceiveEmail,QString SubjectTitle,QString Content)
{
QString toName = ReceiveName;
QString email = ReceiveEmail;
QString subject = SubjectTitle;
QString body = Content;
QString smtpServer = SmtpServer;
int smtpPort = SmtpPort;
QString fromName = SenderName;
QString senderEmail = SenderEmail;
QString senderPassword = SenderPassword;
// Connect to the SMTP server
QTcpSocket socket;
socket.connectToHost(smtpServer, smtpPort);
if (!socket.waitForConnected())
{
qDebug() << "Failed to connect to SMTP server.";
return;
}
else
qDebug() << "Success to connect to SMTP server.";
// Read the welcome message from the server
if (!socket.waitForReadyRead())
{
qDebug() << "Failed to receive welcome message from SMTP server.";
return;
}
QByteArray response = socket.readAll();
qDebug() << response;
// Send the EHLO command
socket.write("EHLO localhost\r\n");
if (!socket.waitForBytesWritten())
{
qDebug() << "Failed to send EHLO command to SMTP server.";
return;
}
if (!socket.waitForReadyRead())
{
qDebug() << "Failed to receive EHLO command response from SMTP server.";
return;
}
response = socket.readAll();
qDebug() << response;
// Send the authentication login command and credentials
socket.write("AUTH LOGIN\r\n");
if (!socket.waitForBytesWritten())
{
qDebug() << "Failed to send AUTH LOGIN command to SMTP server.";
return;
}
if (!socket.waitForReadyRead())
{
qDebug() << "Failed to receive AUTH LOGIN command response from SMTP server.";
return;
}
response = socket.readAll();
qDebug() << response;
QByteArray encodedEmail = senderEmail.toUtf8().toBase64();
QByteArray encodedPassword = senderPassword.toUtf8().toBase64();
socket.write(encodedEmail + "\r\n");
if (!socket.waitForBytesWritten())
{
qDebug() << "Failed to send encoded email to SMTP server.";
return;
}
if (!socket.waitForReadyRead())
{
qDebug() << "Failed to receive email authentication response from SMTP server.";
return;
}
response = socket.readAll();
qDebug() << response;
socket.write(encodedPassword + "\r\n");
if (!socket.waitForBytesWritten())
{
qDebug() << "Failed to send encoded password to SMTP server.";
return;
}
if (!socket.waitForReadyRead())
{
qDebug() << "Failed to receive password authentication response from SMTP server.";
return;
}
response = socket.readAll();
qDebug() << response;
// Send the MAIL FROM command
socket.write(QString("MAIL FROM:<%1>\r\n").arg(senderEmail).toUtf8());
if (!socket.waitForBytesWritten())
{
qDebug() << "Failed to send MAIL FROM command to SMTP server.";
return;
}
if (!socket.waitForReadyRead())
{
qDebug() << "Failed to receive MAIL FROM command response from SMTP server.";
return;
}
response = socket.readAll();
qDebug() << response;
// Send the RCPT TO command
socket.write(QString("RCPT TO:<%1>\r\n").arg(email).toUtf8());
if (!socket.waitForBytesWritten())
{
qDebug() << "Failed to send RCPT TO command to SMTP server.";
return;
}
if (!socket.waitForReadyRead())
{
qDebug() << "Failed to receive RCPT TO command response from SMTP server.";
return;
}
response = socket.readAll();
qDebug() << response;
// Send the DATA command
socket.write("DATA\r\n");
if (!socket.waitForBytesWritten())
{
qDebug() << "Failed to send DATA command to SMTP server.";
return;
}
if (!socket.waitForReadyRead())
{
qDebug() << "Failed to receive DATA command response from SMTP server.";
return;
}
response = socket.readAll();
qDebug() << response;
// Send the message headers and body
QString message = "To: \"" + toName + "\"<" + email + ">\r\n";
message += "From: \"" + fromName + "\"<" + senderEmail + ">\r\n";
message += "Subject: " + subject + "\r\n";
message += "Content-Type: text/plain; charset=\"utf-8\"\r\n\r\n";
message += body + "\r\n";
socket.write(message.toUtf8());
socket.write(".\r\n");
if (!socket.waitForBytesWritten())
{
qDebug() << "Failed to send message to SMTP server.";
return;
}
qDebug() << "Message sent successfully.";
// Quit the session
socket.write("QUIT\r\n");
socket.waitForBytesWritten();
// Close the connection
socket.close();
}