0

I have a problem described and solved for gmail in this post. I have read through several posts dealing with similar issues (Sending mail from Python using SMTP, Sending email from Python using STARTTLS, smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful')) and also some external support on Microsoft (eg. send email using Microsoft 365 but in none I was able to figure out how to fix my error.

My code is this:

import getpass
import smtplib

mailserver = smtplib.SMTP('smtp.office365.com',587)
mailserver.ehlo()
mailserver.starttls()

a = getpass.getpass('Password: ')
mailserver.login('name@domain.com', a)

and connection is established (ie. I get 250 for .ehlo and 220 for .starttls) but the last step gives me:

SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful [LO4P123CA0352.GBRP123.PROD.OUTLOOK.COM]')

The documentation says:

SMTP authentication went wrong. Most probably the server didn’t accept the username/password combination provided.

I am sure that my password and username are correct (triple checked that). Also, I looked to my account and in the Microsoft account it shows that POP and IMAP are allowed and it gives me:

Server name: smtp.office365.com
Port: 587
Encryption method: STARTTLS

so everything should be ok. I have also tried port 25 which gives the same error. Where is the problem? How can I make it work for Outlook/Microsoft 365? Thanks for any help.

My Work
  • 810
  • 1
  • 4
  • 25

0 Answers0