How to send mail using python using an outlook sender mail with two factor authentication

Anonymous
2023-02-07T08:22:36+00:00

I have tried sending mail using smtp using the guide given here : https://realpython.com/python-send-email/ but the program was stuck without giving any output or error. After a bit of debugging I found that it was stuck at the login phase and I figured that it could be due to the two factor authentication(uses Duo OTP) hence I would like to know what can be done in this situation when the sender and receiver both are using Outlook.

I tried searching on how we can send mail using python when there is two factor authentication but could not find any concrete answer.

The goal is to send a mail(using python) from outlook account to another outlook account when there is two factor authentication enabled for log in.

The code that I have tried is :

import smtplib, ssl *print("Running mail file")port = 465 # For SSLsmtp_server = "smtp.office365.com"sender_email = "" # Enter your addressreceiver_email = ""# Enter receiver address**password = input("Type your password and press enter: ")**message = """* Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context() *print("before login")**with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:**server.login(sender_email, password)**print("logged in")**server.sendmail(sender_email, receiver_email, message)*print("sent the mail")

Outlook | MacOS | Legacy Outlook for Mac | For education

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Anonymous
    2023-02-07T11:39:39+00:00

    Hello explorer03,

    Good day!

    Thank you for posting to Microsoft Community. We are happy to help you.

    Based on you description i would like to inform you below is a comparison of each configuration option and feature we support.

    Reference: How to set up a multifunction device or application to send email using Microsoft 365 or Office 365 | Microsoft Learn

    And i can see that the setting from python doesn't match any features Microsoft suggest setting up application to send email using Microsoft 365 or Office 365. It looks like phyton doesn't use TLS. I suggest you contact phyton support and show them our feature we suggest to any application.

    Please feel free to let us know if there are any questions or if we've got you wrong. We will keep assisting you based on the information you provide. We sincerely appreciate your patience and cooperation.

    Thanks for your precious time.

    Sincerely,

    Eben Ezer Tres | Microsoft Community Moderator

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2023-02-08T07:02:45+00:00

    Thank you for the reply, I was unable to get any clear answer from the link that was shared. So do you mean it is not possible with the current python support to send mail using an outlook mail account which has two factor authentication?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2023-02-08T08:07:55+00:00

    Hello explorer03,

    Thank you for your reply,

    So do you mean it is not possible with the current python support to send mail using an outlook mail account which has two factor authentication? it is not possible because we have each configuration option and feature we suggest to use for that kind a issue.

    Also, smtp_server = "smtp.office365.com" is for smtp auth and the required port will be 587

    You can't use any random port to connect; if you are agrees to use smtp auth, you should consider testing the service using the PS cmd to see if it works for you or not: 

    Send-MailMessage -SmtpServer smtp.office365.com -Port 587 -From ******@domain.com -To ******@domain.com -Subject "SMTP Auth Client $(Get-Date -Format g)" -Body "This is a test email using SMTP Auth" -UseSSL

    If it doesn't work you should contact python support, to see if there is not another way, in order to respect our feature.

    Appreciate your patience and understanding and thank you for your time and cooperation.

    Sincerely,

    Eben Ezer Tres | Microsoft Community Moderator

    0 comments No comments
  4. Anonymous
    2023-02-08T08:33:39+00:00

    Thank you for the detailed reply but I did try with the port number 587. But I switched back to 465 after it gave an error. I will try contacting python support as you have mentioned.

    Thanking you,

    explorer03

    0 comments No comments
  5. Anonymous
    2023-02-08T09:22:53+00:00

    Hello explorer03,

    Thank you for your reply,

    Thanks for your valuable feedback and please can you vote the first suggestion it will help the others users who want use any application to send email using Microsoft 365 or Office 365, the link will be helpful for them .

    Comments and feedback from our customers really motivate us to perform better and better and provided as much help as possible.

    I appreciate your kind words.

    If there is any issue related with the Office applications in the future, please feel free to post back in this community.

    We will be here for help.

    Have a good day and stay safe

    Sincerely,

    Eben Ezer Tres | Microsoft Community Moderator

    0 comments No comments