Search code examples
emailoutlooksmtpsmtp-authmicrosoft365

Microsoft 365 Business Basic SMTP Authentication Failure


I have a custom Microsoft 365 business email domain and an email account of [email protected]. Using the Python code below I am not able to send an email to another user due to an SMTP authentication failure:

import smtplib, ssl

port = 587  # For starttls
smtp_server = 'smtp.office365.com'
sender_email = '[email protected]'
receiver_email = '[email protected]'
password = 'mysecretpassword'
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    server.ehlo()  # Can be omitted
    server.starttls(context=context)
    server.ehlo()  # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)`

The exact error is as follows:

smtplib.SMTPAuthenticationError: (535, b'5.7.139 Authentication unsuccessful, the request did not meet the criteria to be authenticated successfully. Contact your administrator

Things I've tried:

  1. Checked that the email and password are correct.
  2. Verified that there is no firewall issue and that there is nothing wrong with the code above by swapping the sender and receiver emails (and of course changing the password too) - in this case it works normally and the authentication succeeds. Since the domain was hotmail.ca the SMTP server was changed to smtp-mail.outlook.com too.
  3. Verified SMTP settings via outlook settings -> Mail -> Sync Email -> SMTP setting
  4. Checked the Settings -> Mail Flow -> Turn Off SMTP AUTH Protocol (unchecked)
  5. Checked the Active Users -> [email protected] -> Mail -> Managed Email Apps -> Authenticated SMTP (checked)
  6. Checked that multifactor authentication is disabled, thereby not needing an app key
  7. Added a connector from my organizations email server to office 365 with a domain name of *.secretdomain.ca

As you can see I've gone above and beyond what one should reasonably need to do to get SMTP functional, but Microsoft does not seem to go above and beyond to make sure their service is easy to use. What have I (or they) done wrong?


Solution

  • The SMTP authentication is disabled on the remote server. See https://aka.ms/smtp_auth_disabled for more information.

    Also you may find the following setting helpful that could block authentication:

    Azure settings

    See Error: Authentication unsuccessful for more information.