Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

email - Sending mail with python SMTP to outlook/microsoft gives SMTPAuthenticationError: Authentication unsuccessful

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('[email protected]', 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...