pythonemailexchange-server-2010

How to get all mails from MS exchange in Python?


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.


Solution

  • 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)