def main():
username = os.environ.get("EMAIL_ADDRESS")
password = os.environ.get("EMAIL_PASSWORD")
imap_server = 'outlook.office365.com'
# create an IMAP4 class with SSL
imap = imaplib.IMAP4_SSL(imap_server, port=993)
print(imap)
# authenticate
imap.login(username, password)
print(f"Successfully connected to {imap_server} on port 993")
print(f"Logged in as {username}")
# Print IMAP server capabilities
print("IMAP Server Capabilities:")
for capability in imap.capabilities:
logger.info(f" {capability}")
# Print selected mailbox information
status, mailbox_info = imap.select('INBOX')
if status == 'OK':
messages = int(mailbox_info[0])
print(f"Selected mailbox: INBOX")
print(f"Number of messages: {messages}")
# Iterate over the emails
for _ in range(messages, max(0, messages - 10), -1): # Process the last 10 emails or fewer
status, msg_data = imap.fetch(_, "(RFC822)")
raw_email = msg_data[0][1]
msg = email.message_from_bytes(raw_email)
else:
print("Failed to select INBOX")
if name == "main": main()
here I'm using correct mail and app password and also tried with mail password both are getting same error--- " IMAP4 error: b'LOGIN failed."
how to resolve imap server login issue outlook and this outlook also have MFA.