I created a registered app in Azure and getting the access token issued to me via Postman.
When I receive a token from Azure with "client credentials" as the grant type, the Python script that I am testing with seems to be working fine and sends a test email.
However, if I try to use the "authorization_code" as the grant type when the token is issued, I get the error in the title "535 5.7.3 Authentication unsuccessful".
SMTP is allowed on the mailbox I am trying to use No CA policy blocking that I can see SMTP.SEND permission has been granted to the app and admin consent provided
I am able to get the token issued just fine but why is it not able to authentication when using the auth code grant type?
Currently have a ticket open with MS and waiting escalation as the general support team because we are using a script to test, they don't support it...
import smtplib import base64 import os import ssl smtp_server = "smtp.office365.com" #smtp-mail.outlook.com #smtp.office365.com smtp_port = 587 from_addr = "xxxxxxxx" #os.getenv("SMTP_USER") to_addr = "xxxxxx" access_token = "xxxxxxxx" #os.getenv("SMTP_ACCESS_TOKEN") if not access_token: print("Error: Access token not set in SMTP_ACCESS_TOKEN") exit(1) # Compose XOAUTH2 string auth_string = f"user={from_addr}\x01auth=Bearer {access_token}\x01\x01" auth_b64 = base64.b64encode(auth_string.encode('utf-8')).decode('utf-8') try: smtp = smtplib.SMTP(smtp_server, smtp_port) smtp.set_debuglevel(1) # Enable verbose logging smtp.ehlo() smtp.starttls() smtp.ehlo() code, resp = smtp.docmd("AUTH", "XOAUTH2 " + auth_b64) print("AUTH response:", code, resp.decode('utf-8')) if code != 235: print("Failed XOAUTH2 Auth!") smtp.quit() exit(1) msg = f"From: {from_addr}\r\nTo: {to_addr}\r\nSubject: Test mail via SMTP OAuth2!\r\n\r\nThis is a test." smtp.sendmail(from_addr, [to_addr], msg) print("Mail sent!") except smtplib.SMTPException as e: print(f"SMTP error: {e}") except Exception as e: print(f"Unexpected error: {e}") finally: smtp.quit()