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:
- Enable SMTP Auth (this is enabled)
- 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!