How can I run an email listener with exchangelib that triggers when a new mail is received?

Viewed 2300

I want to write a service that takes a message from my outlook account. For connection to outlook, I used exchangelib but I didn't find how to run listener for incoming emails. Here is my code.

from exchangelib import DELEGATE, Account, Credentials
from creds import PASSWORD, USERNAME, EMAIL

creds = Credentials(
    username=USERNAME,
    password=PASSWORD)
account = Account(
    primary_smtp_address=EMAIL,
    credentials=creds,
    autodiscover=True,
    access_type=DELEGATE)

# Print first 100 inbox messages in reverse order
for item in account.inbox.all().order_by('-datetime_received')[:1]:
    print(item.subject, item.body, item.attachments)

It's a simple example that I found and it return to me last message from my incoming mailbox.

Please, can you give me some link where I can find a solution or help me to write listener service?

1 Answers
Related