3

I am trying to send mail via python script using the following code

import smtplib


def print_hi(name):
    sender = 'my@mail.com'
    receivers = ['receiver@mail.com']

    message = """some msg"""

    server = smtplib.SMTP('smtp.office365.com', 587)
    server.set_debuglevel(1)
    server.starttls()
    server.ehlo()
    server.login("username", "password")
    server.sendmail(sender, receivers, message)
    server.quit()


if __name__ == '__main__':
    print_hi('PyCharm')

I am getting the following error:

  raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.139 Authentication unsuccessful, the request did not meet the criteria to be authenticated successfully. Contact your administrator. [BM1PR01CA0144.INDPRD01.PROD.OUTLOOK.COM]')

I looked into many StackOverflow questions and Microsoft blogs and got the following steps to enable SMTP for our organization:

  1. Enable SMTP Auth (this is enabled)
  2. Check security defaults (we cannot disable it as it disables all MFA and this won't comply at the organization level)

So if anyone knows how to make it work keeping in mind all the security it would be helpful!

Mayuresh
  • 61
  • 1
  • 2
  • Have you created an app password for the account you want to use? I would assume that by default plain password authentication is disabled for your account, instead SMTP expects oAuth authentication. – Robert Jun 01 '22 at 07:34
  • Yes even tried with App password But still no luck. Same error. – Mayuresh Jun 01 '22 at 08:07
  • Does this answer your question? [smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful')](https://stackoverflow.com/questions/38602682/smtplib-smtpauthenticationerror-535-5-7-3-authentication-unsuccessful) – Eugene Astafiev Jun 05 '22 at 13:48

0 Answers0