Better error handling with smtp communication.

Adds log messages for a number of situations where sending an email will silently fail, in particular when attempting to create an unencrypted connection to an SMTP server that requires SSL
This commit is contained in:
Brian Kendall 2016-07-18 17:55:36 -04:00
parent 630871214f
commit da412571e4
2 changed files with 23 additions and 0 deletions

View file

@ -97,6 +97,13 @@ Smtp::Smtp(QObject *parent)
, m_useSsl(false)
, m_authType(AuthPlain)
{
static bool needToRegisterMetaType = true;
if (needToRegisterMetaType) {
qRegisterMetaType<QAbstractSocket::SocketError>();
needToRegisterMetaType = false;
}
#ifndef QT_NO_OPENSSL
m_socket = new QSslSocket(this);
#else
@ -105,6 +112,7 @@ Smtp::Smtp(QObject *parent)
connect(m_socket, SIGNAL(readyRead()), SLOT(readyRead()));
connect(m_socket, SIGNAL(disconnected()), SLOT(deleteLater()));
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(error(QAbstractSocket::SocketError)));
// Test hmacMD5 function (http://www.faqs.org/rfcs/rfc2202.html)
Q_ASSERT(hmacMD5("Jefe", "what do ya want for nothing?").toHex()
@ -525,3 +533,11 @@ QString Smtp::getCurrentDateTime() const
QString ret = weekDayStr + ", " + dayStr + " " + monthStr + " " + yearStr + " " + timeStr + " " + timeOffsetStr;
return ret;
}
void Smtp::error(QAbstractSocket::SocketError socketError)
{
// Getting a remote host closed error is apparently normal, even when successfully sending
// an email
if (socketError != QAbstractSocket::RemoteHostClosedError)
logError(m_socket->errorString());
}

View file

@ -39,6 +39,8 @@
#include <QObject>
#include <QByteArray>
#include <QHash>
#include <QAbstractSocket>
#include <QMetaType>
QT_BEGIN_NAMESPACE
class QTextStream;
@ -64,6 +66,7 @@ namespace Net
private slots:
void readyRead();
void error(QAbstractSocket::SocketError socketError);
private:
enum States
@ -123,4 +126,8 @@ namespace Net
};
}
#ifndef QBT_USES_QT5
Q_DECLARE_METATYPE(QAbstractSocket::SocketError)
#endif
#endif