Home > Software engineering >  authentication error trying to send Outlook email from Python
authentication error trying to send Outlook email from Python

Time:06-30

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.

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?

CodePudding user response:

The error indicates that SMTP authentication is disabled. Read more about that on the page at settings that block Legacy Authentication in outlook

  • Related