Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POCO (class Poco::Net::SMTPException - 0): SMTP Exception: The mail service does not support XOAUTH2 authentication #4858

Open
Vin745 opened this issue Jan 28, 2025 · 2 comments

Comments

@Vin745
Copy link

Vin745 commented Jan 28, 2025

I am trying to send an email using OAUTH2.0 Authentication type, using Poco 1.11.0 version, but I am getting the below expectation.

Your email system reported an error sending mail. details: [POCO (class Poco::Net::SMTPException - 0): SMTP Exception: The mail service does not support XOAUTH2 authentication: 250-MA1PEPF00007263.mail.protection.outlook.com Hello [XXX.XXX.XXX.XXX]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8]

Please review the code snippet below and let me know your thoughts.

#include
#include
#include <Poco/JSON/Parser.h>
#include "Poco/Net/MailMessage.h"
#include "Poco/Net/MailRecipient.h"
#include "Poco/Net/SMTPClientSession.h"
#include "Poco/Net/StringPartSource.h"
#include "Poco/Net/NetException.h"
#include "Poco/Net/SecureSMTPClientSession.h"
#include "Poco/Net/SslManager.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/HTTPResponse.h"
#include <Poco/Net/AcceptCertificateHandler.h>
#include <Poco/Net/Context.h>

int main(int argc, char** argv)
{
string AuthToken = //Getting from other function.
string smtpHost = "Servername";
Poco::UInt16 port = 25;
try {
// Initialize SSL Context for STARTTLS
// POCO SSL initialization.
std::shared_ptr sessionPtr;

	Poco::SharedPtr<InvalidCertHandler> invalidCertHandler(new InvalidCertHandler());
	Context::Ptr contextPtr = new Context(
		Context::TLS_CLIENT_USE,
		"",
		Context::VerificationMode::VERIFY_STRICT,
		Context::OPT_PERFORM_REVOCATION_CHECK | Context::OPT_TRUST_ROOTS_WIN_CERT_STORE | Context::OPT_USE_STRONG_CRYPTO);

	contextPtr->disableProtocols(Context::PROTO_SSLV2 | Context::PROTO_SSLV3 | Context::PROTO_TLSV1 | Context::PROTO_TLSV1_1 | Context::PROTO_TLSV1_3);

	SSLManager::instance().initializeClient(nullptr, invalidCertHandler, contextPtr);

	std::shared_ptr<SecureSMTPClientSession> secureSessionPtr(new SecureSMTPClientSession(smtpHost, port));
	secureSessionPtr->login(Poco::Net::SMTPClientSession::AUTH_XOAUTH2, sender_email, AuthToken);
	secureSessionPtr->startTLS();
	sessionPtr = secureSessionPtr;
}
catch (const Poco::Exception& e)
{
	const Poco::Net::SMTPException* pSmtpExc = dynamic_cast<const Poco::Net::SMTPException*>(&e);
	if (pSmtpExc)
	{
		int errorClass = e.code() / 100;
		if (errorClass == 5)
		{
			//shouldRetry = false;
		}
	}

	string pocoMsg = e.message();
	if (pocoMsg.empty() && !pSmtpExc && e.code() != 0)
	{
		pocoMsg = e.code();
	}

	string msg = "[POCO ({0} - {1}): {2}]";
	replaceAll(msg, "{0}", e.className());
	replaceAll(msg, "{1}", to_string(e.code()));
	replaceAll(msg,"{2}", e.name() + string(": ") + pocoMsg);
	//AfxMessageBox(msg.c_str());

}

}

@matejk
Copy link
Contributor

matejk commented Jan 28, 2025

Does it work with the latest version of Poco?

@Vin745
Copy link
Author

Vin745 commented Jan 30, 2025

Did you mean the 1.14.0 version? I haven't tried it, but I can give it a try.

Poco 1.11.0 doesn't support OAuth 2.0, let me know if you have any idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants