Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

authentication error trying to send Outlook email from Python

I’m testing out a simple script to send an Outlook email from Python 3 (using Spyder).

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

username = 'my_username@my_company.com'
password = 'my_password'

mail_from = username
mail_to = username
mail_subject = "Test Subject"
mail_body = "This is a test message"

mimemsg = MIMEMultipart()
mimemsg['From']=mail_from
mimemsg['To']=mail_to
mimemsg['Subject']=mail_subject
mimemsg.attach(MIMEText(mail_body, 'plain'))

try:
    connection = smtplib.SMTP(host='smtp.office365.com', port=587)
    connection.starttls()
    connection.login(username,password)
except Exception as e:
    print('Got error here')
    print(e)

And the output is:

Got error here
(535, b'Authentication unsuccessful, the user credentials were incorrect. [SOME_VALUE_HERE.hostname.prod.outlook.com]')

I know for sure my own username and email are correct – I verified by checking my username’s properties > SMTP value. And anyway it’s the username I use to login to Windows.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m also using the same password for logging into Windows.

Is it possible my company uses different values for host or port? Or on the backend it sends a different user name to the SMTP server?

>Solution :

The error indicates that SMTP authentication is disabled. Read more about that on the page at https://aka.ms/smtp_auth_disabled. The link explains how to enable SMTP AUTH for the whole organization or only for some mailboxes.

Also take a look at the following settings that would block Legacy Authentication:

settings that block Legacy Authentication in outlook

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading