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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/*
Copyright (c) 2011-2012 - Tőkés Attila
This file is part of SmtpClient for Qt.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
See the LICENSE file for more details.
*/
#include "smtp/smtpclient.h"
#include <QByteArray>
#include <QFileInfo>
/* [1] Constructors and destructors */
SmtpClient::SmtpClient(const QString &host, int port,
ConnectionType connectionType)
: socket(NULL), name("localhost"), authMethod(AuthPlain),
connectionTimeout(5000), responseTimeout(5000),
sendMessageTimeout(60000) {
setConnectionType(connectionType);
this->host = host;
this->port = port;
connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this,
SLOT(socketStateChanged(QAbstractSocket::SocketState)));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this,
SLOT(socketError(QAbstractSocket::SocketError)));
connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
}
SmtpClient::~SmtpClient() {
if (socket)
delete socket;
}
/* [1] --- */
/* [2] Getters and Setters */
void SmtpClient::setUser(const QString &user) { this->user = user; }
void SmtpClient::setPassword(const QString &password) {
this->password = password;
}
void SmtpClient::setAuthMethod(AuthMethod method) { this->authMethod = method; }
void SmtpClient::setHost(const QString &host) { this->host = host; }
void SmtpClient::setPort(int port) { this->port = port; }
void SmtpClient::setConnectionType(ConnectionType ct) {
this->connectionType = ct;
if (socket)
delete socket;
switch (connectionType) {
case TcpConnection:
socket = new QTcpSocket(this);
break;
case SslConnection:
case TlsConnection:
socket = new QSslSocket(this);
}
}
const QString &SmtpClient::getHost() const { return this->host; }
const QString &SmtpClient::getUser() const { return this->user; }
const QString &SmtpClient::getPassword() const { return this->password; }
SmtpClient::AuthMethod SmtpClient::getAuthMethod() const {
return this->authMethod;
}
int SmtpClient::getPort() const { return this->port; }
SmtpClient::ConnectionType SmtpClient::getConnectionType() const {
return connectionType;
}
const QString &SmtpClient::getName() const { return this->name; }
void SmtpClient::setName(const QString &name) { this->name = name; }
const QString &SmtpClient::getResponseText() const { return responseText; }
int SmtpClient::getResponseCode() const { return responseCode; }
QTcpSocket *SmtpClient::getSocket() { return socket; }
int SmtpClient::getConnectionTimeout() const { return connectionTimeout; }
void SmtpClient::setConnectionTimeout(int msec) { connectionTimeout = msec; }
int SmtpClient::getResponseTimeout() const { return responseTimeout; }
void SmtpClient::setResponseTimeout(int msec) { responseTimeout = msec; }
int SmtpClient::getSendMessageTimeout() const { return sendMessageTimeout; }
void SmtpClient::setSendMessageTimeout(int msec) { sendMessageTimeout = msec; }
/* [2] --- */
/* [3] Public methods */
bool SmtpClient::connectToHost() {
switch (connectionType) {
case TlsConnection:
case TcpConnection:
socket->connectToHost(host, port);
break;
case SslConnection:
((QSslSocket *) socket)->connectToHostEncrypted(host, port);
break;
}
// Tries to connect to server
if (!socket->waitForConnected(connectionTimeout)) {
emit smtpError(ConnectionTimeoutError);
return false;
}
try {
// Wait for the server's response
waitForResponse();
// If the response code is not 220 (Service ready)
// means that is something wrong with the server
if (responseCode != 220) {
emit smtpError(ServerError);
return false;
}
// Send a EHLO/HELO message to the server
// The client's first command must be EHLO/HELO
sendMessage("EHLO " + name);
// Wait for the server's response
waitForResponse();
// The response code needs to be 250.
if (responseCode != 250) {
emit smtpError(ServerError);
return false;
}
if (connectionType == TlsConnection) {
// send a request to start TLS handshake
sendMessage("STARTTLS");
// Wait for the server's response
waitForResponse();
// The response code needs to be 220.
if (responseCode != 220) {
emit smtpError(ServerError);
return false;
};
((QSslSocket *) socket)->startClientEncryption();
if (!((QSslSocket *) socket)->waitForEncrypted(connectionTimeout)) {
qDebug() << ((QSslSocket *) socket)->errorString();
emit smtpError(ConnectionTimeoutError);
return false;
}
// Send ELHO one more time
sendMessage("EHLO " + name);
// Wait for the server's response
waitForResponse();
// The response code needs to be 250.
if (responseCode != 250) {
emit smtpError(ServerError);
return false;
}
}
} catch (ResponseTimeoutException) {
return false;
} catch (SendMessageTimeoutException) {
return false;
}
// If no errors occured the function returns true.
return true;
}
bool SmtpClient::login() { return login(user, password, authMethod); }
bool SmtpClient::login(const QString &user, const QString &password,
AuthMethod method) {
try {
if (method == AuthPlain) {
// Sending command: AUTH PLAIN base64('\0' + username + '\0' + password)
sendMessage("AUTH PLAIN " + QByteArray()
.append((char) 0)
.append(user.toUtf8())
.append((char) 0)
.append(password.toUtf8())
.toBase64());
// Wait for the server's response
waitForResponse();
// If the response is not 235 then the authentication was faild
if (responseCode != 235) {
emit smtpError(AuthenticationFailedError);
return false;
}
} else if (method == AuthLogin) {
// Sending command: AUTH LOGIN
sendMessage("AUTH LOGIN");
// Wait for 334 response code
waitForResponse();
if (responseCode != 334) {
emit smtpError(AuthenticationFailedError);
return false;
}
// Send the username in base64
sendMessage(QByteArray().append(user.toUtf8()).toBase64());
// Wait for 334
waitForResponse();
if (responseCode != 334) {
emit smtpError(AuthenticationFailedError);
return false;
}
// Send the password in base64
sendMessage(QByteArray().append(password.toUtf8()).toBase64());
// Wait for the server's responce
waitForResponse();
// If the response is not 235 then the authentication was faild
if (responseCode != 235) {
emit smtpError(AuthenticationFailedError);
return false;
}
}
} catch (ResponseTimeoutException) {
// Responce Timeout exceeded
emit smtpError(AuthenticationFailedError);
return false;
} catch (SendMessageTimeoutException) {
// Send Timeout exceeded
emit smtpError(AuthenticationFailedError);
return false;
}
return true;
}
bool SmtpClient::sendMail(MimeMessage &email) {
try {
// Send the MAIL command with the sender
sendMessage("MAIL FROM:<" + email.getSender().getAddress() + ">");
waitForResponse();
if (responseCode != 250)
return false;
// Send RCPT command for each recipient
QList<EmailAddress *>::const_iterator it, itEnd;
// To (primary recipients)
for (it = email.getRecipients().begin(),
itEnd = email.getRecipients().end();
it != itEnd; ++it) {
sendMessage("RCPT TO:<" + (*it)->getAddress() + ">");
waitForResponse();
if (responseCode != 250)
return false;
}
// Cc (carbon copy)
for (it = email.getRecipients(MimeMessage::Cc).begin(),
itEnd = email.getRecipients(MimeMessage::Cc).end();
it != itEnd; ++it) {
sendMessage("RCPT TO:<" + (*it)->getAddress() + ">");
waitForResponse();
if (responseCode != 250)
return false;
}
// Bcc (blind carbon copy)
for (it = email.getRecipients(MimeMessage::Bcc).begin(),
itEnd = email.getRecipients(MimeMessage::Bcc).end();
it != itEnd; ++it) {
sendMessage("RCPT TO:<" + (*it)->getAddress() + ">");
waitForResponse();
if (responseCode != 250)
return false;
}
// Send DATA command
sendMessage("DATA");
waitForResponse();
if (responseCode != 354)
return false;
sendMessage(email.toString());
// Send \r\n.\r\n to end the mail data
sendMessage(".");
waitForResponse();
if (responseCode != 250)
return false;
} catch (ResponseTimeoutException) {
return false;
} catch (SendMessageTimeoutException) {
return false;
}
return true;
}
void SmtpClient::quit() {
try {
sendMessage("QUIT");
} catch (SmtpClient::SendMessageTimeoutException) {
// Manually close the connection to the smtp server if message "QUIT" wasn't
// received by the smtp server
if (socket->state() == QAbstractSocket::ConnectedState ||
socket->state() == QAbstractSocket::ConnectingState ||
socket->state() == QAbstractSocket::HostLookupState)
socket->disconnectFromHost();
}
}
/* [3] --- */
/* [4] Protected methods */
void SmtpClient::waitForResponse() {
do {
if (!socket->waitForReadyRead(responseTimeout)) {
emit smtpError(ResponseTimeoutError);
throw ResponseTimeoutException();
}
while (socket->canReadLine()) {
// Save the server's response
responseText = socket->readLine();
// Extract the respose code from the server's responce (first 3 digits)
responseCode = responseText.leftRef(3).toInt();
if (responseCode / 100 == 4)
emit smtpError(ServerError);
if (responseCode / 100 == 5)
emit smtpError(ClientError);
if (responseText[3] == ' ') {
return;
}
}
} while (true);
}
void SmtpClient::sendMessage(const QString &text) {
socket->write(text.toUtf8() + "\r\n");
if (!socket->waitForBytesWritten(sendMessageTimeout)) {
emit smtpError(SendDataTimeoutError);
throw SendMessageTimeoutException();
}
}
/* [4] --- */
/* [5] Slots for the socket's signals */
void SmtpClient::socketStateChanged(QAbstractSocket::SocketState /*state*/) {}
void SmtpClient::socketError(QAbstractSocket::SocketError /*socketError*/) {}
void SmtpClient::socketReadyRead() {}
/* [5] --- */
|