Hi,
We can read emails using IMAP. Let me show an example.
from exchangelib import Credentials, Account
email_address = "your-email@example.com"
password = "your-password"
credentials = Credentials(email_address, password)
account = Account(email_address, credentials=credentials, autodiscover=True)
for item in account.inbox.all().order_by('-datetime_received')[:5]:
print(item.subject, item.datetime_received)
It does not work for office365 email.
There is also another piece of code.
# IMAP server settings
IMAP_SERVER = "outlook.office365.com"
USERNAME ="******@email.com"
PASSWORD = "password"
# Connect to the IMAP server
imap = imaplib.IMAP4(IMAP_SERVER, port=OUTLOOK_IMAP_PORT)
imap.login(USERNAME, PASSWORD)
But this one also does not work.
- Error: {error_proto}b'-ERR Logon failure: unknown user name or bad password.' (I used the password where 2FA is enabled.)
- {error}b'LOGIN failed.' (This time, I used third-party app password)
I can read email from my personal Outlook account using the above piece of code but cannot read from office365 mail.
Is the IMAP disabled completely by the office365?
Or I can enable it from somewhere in the admin site and use the above code to read emails from the server?