diff --git a/include/ulib/net/client/smtp.h b/include/ulib/net/client/smtp.h index 5315f1af..c859aac2 100644 --- a/include/ulib/net/client/smtp.h +++ b/include/ulib/net/client/smtp.h @@ -69,6 +69,8 @@ public: GREET = 220, // greeting from server GOODBYE = 221, // server acknolages quit SUCCESSFUL = 250, // command successful + AUTHENTICATED = 235, // Authentication successful + CHALLENGE = 334, // Login Credential Reqested READYDATA = 354, // server ready to receive data UNAVAILABLE = 450, // service not available ERR_PROCESSING = 451, // error in processing @@ -116,7 +118,8 @@ public: UString getRecipientAddress() const { return rcptoAddress; } bool startTLS(); - bool sendMessage(bool secure = false); + bool authLogin(const UString* username, const UString* password); + bool sendMessage(bool secure = false, const UString* username = U_NULLPTR, const UString* password = U_NULLPTR); void setDomainName( const UString& name) { domainName = name; } void setMessageBody( const UString& message) { messageBody = message; } diff --git a/src/ulib/net/client/smtp.cpp b/src/ulib/net/client/smtp.cpp index aab2233b..86461fdf 100644 --- a/src/ulib/net/client/smtp.cpp +++ b/src/ulib/net/client/smtp.cpp @@ -172,8 +172,10 @@ U_NO_EXPORT void USmtpClient::setStateFromResponse() { case -1: state = CERROR; break; case GREET: state = LOG_IN; break; + case CHALLENGE: state = LOG_IN; break; case GOODBYE: state = QUIT; break; case READYDATA: state = DATA; break; + case AUTHENTICATED: response = SUCCESSFUL; break; case SUCCESSFUL: { @@ -237,9 +239,31 @@ bool USmtpClient::startTLS() U_RETURN(false); } +bool USmtpClient::authLogin(const UString* username, const UString* password) +{ + U_TRACE_NO_PARAM(0, "USmtpClient::auth_login()") + syncCommand(U_CONSTANT_TO_PARAM("HELO")); + +#ifdef USE_LIBSSL + if (((USSLSocket*)this)->isSSLActive() + && username->isBase64() && password->isBase64()){ + + if (syncCommand(U_CONSTANT_TO_PARAM("auth login")) + && syncCommand(username->c_str(),username->size()) + && syncCommand(password->c_str(),password->size()) + && response == SUCCESSFUL){ + U_RETURN(true); + } + } +#endif + + U_RETURN(false); +} + + // Execute an smtp transaction -bool USmtpClient::sendMessage(bool secure) +bool USmtpClient::sendMessage(bool secure, const UString* username, const UString* password) { U_TRACE(0, "USmtpClient::sendMessage(%b)", secure) @@ -267,9 +291,18 @@ bool USmtpClient::sendMessage(bool secure) { U_RETURN(false); } + else + { + if(username && password) + { + if(!authLogin(username,password)){ + U_RETURN(false); + } + } (void) syncCommand(U_CONSTANT_TO_PARAM("ehlo %v"), domainName.rep); } + } if (response != SUCCESSFUL) U_RETURN(false);