cannot sign in programmatically with app password

Yibo Liu 20 Reputation points
2025-12-06T21:31:17.24+00:00

Hi there,

I'm trying to programmatically read my outlook email inbox. The code I used is:

mail = imaplib.IMAP4_SSL("outlook.office365.com")
app_password = <Removed for PII>("OUTLOOK_APP_PASSWORD")
print(app_password)
mail.debug = 4
mail.login("<Removed for PII>", app_password)

I always get error

23:07.53 < b'MFKO1 NO LOGIN failed.'
  23:07.53 NO response: b'LOGIN failed.'
   ✗ IMAP Error: b'LOGIN failed.'

I'm sure I've enabled IMAP and POP, and the app_password should be correct. What's wrong with it? If this is not the right way of login, what's your recommended alternative?

Moved from Outlook New Outlook for Mac For Home MacOS

Microsoft 365 and Office | Development | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Kha-N 5,760 Reputation points Microsoft External Staff Moderator
    2025-12-08T01:51:24.3+00:00

    Hi @YiboLiu,

    Welcome to Microsoft Q&A, and thank you so much for reaching out to us.

    Please note that our forum is a public platform, and we will modify your question to hide your personal information in the description. Kindly ensure that you hide any personal or organizational information the next time you post an error or other details to protect personal data.

    Please note that as a Microsoft Q&A moderator, I do not have access to your specific configuration, and my testing environment is limited. However, I’ll do my best to assist you.

    Based on your description, your email appears to be a personal Outlook.com account, and the IMAP server you’re using is incorrect. For personal accounts, the correct IMAP server is: imap-mail.outlook.com

    User's image

    For more information, you can check on this Microsoft Article here.

    Additionally, please be aware that Microsoft has disabled Basic Authentication (username/password) for IMAP, POP, and SMTP across both enterprise and consumer services.

    User's image

    If you want to programmatically log in to your account, the recommended approach is to register your app in Azure or Entra and obtain an OAuth 2.0 access token scoped for IMAP/SMTP. This ensures compliance with modern authentication requirements and security best practices.

    User's image

    For more information, you can check out this Microsoft Article here.

    I hope this help.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".     

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.