Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 648
Ani Menon Avatar asked Apr 22 '16 12:04

Ani Menon


1 Answers

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='[email protected]',
    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)
like image 64
Erik Cederstrand Avatar answered Sep 29 '22 11:09

Erik Cederstrand