7

I want to view all the mails I have received on MS Exchange/OWA. Is there a way to do this using Python?

I do see few solutions in C#/Java.

But how may I do it in Python? A similar question is Connect to exchange with python, but I am not able to understand how to do it.

Community
  • 1
  • 1
Ani Menon
  • 27,209
  • 16
  • 105
  • 126

1 Answers1

17

The Python EWS package I maintain (https://pypi.python.org/pypi/exchangelib) supports this. Here's a simple example:

from exchangelib import DELEGATE, Account, Credentials

creds = Credentials(
    username='MYWINDOMAIN\myusername', 
    password='topsecret')
account = Account(
    primary_smtp_address='john@example.com',
    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')[:100]:
    print(item.subject, item.body, item.attachments)
Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63