If you have a Python script that reads a mailbox in Exchange Online, there is a good chance it uses exchangelib, and that it talks to Exchange Web Services (EWS). That is about to stop working.
Microsoft is retiring EWS in Exchange Online: it is off by default since October 2026, an administrator can still allow it for an app for a while, and on 1 April 2027 it is switched off for good. Exchange servers on premises are not affected. Microsoft's answer for Exchange Online is Microsoft Graph.
exchangelib speaks EWS and will keep doing so. Its maintainer has said that Graph support belongs in a separate package. So what do you do with the code you already have?
Option 1: rewrite it on a Graph library
There are good Graph libraries for Python: Microsoft's own msgraph-sdk, python-o365 and Office365-REST-Python-Client. For new code, pick one of them.
For existing exchangelib code, a rewrite touches more than it looks:
-
Queries.
inbox.filter(is_read=False).order_by("-datetime_received")[:5]becomes an OData$filter/$orderby/$toprequest, and Graph has its own rules about which fields can be filtered and sorted together. - Ids. If you stored item or folder ids from exchangelib, Graph's ids look different, so every stored id needs converting.
- Models. Messages, events, attendees, recurrences and attachments have different shapes and names. Recurring events with modified and deleted occurrences are where most of the surprises are.
- Large attachments. Anything over 3 MB goes through an upload session instead of one request.
None of this is hard on its own. Together, for a script that has worked for years, it is a few days of work plus the bugs you only find in production.
Option 2: keep exchangelib's API, run it on Graph
That is what I built ewsgraph for. It implements exchangelib's API on Microsoft Graph, so for many scripts the migration is one line:
- from exchangelib import IMPERSONATION, OAUTH2, Account, Configuration, Identity, OAuth2Credentials, Q
+ from ewsgraph import IMPERSONATION, OAUTH2, Account, Configuration, Identity, OAuth2Credentials, Q
The rest of the script stays as it is:
credentials = OAuth2Credentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, tenant_id=TENANT_ID,
identity=Identity(primary_smtp_address=MAILBOX))
config = Configuration(server="outlook.office365.com", credentials=credentials, auth_type=OAUTH2)
account = Account(primary_smtp_address=MAILBOX, config=config, autodiscover=False,
access_type=IMPERSONATION)
print("unread:", account.inbox.filter(is_read=False).count())
for msg in account.inbox.filter(Q(importance="High") | Q(is_read=False)).order_by("-datetime_received")[:5]:
print(msg.datetime_received, msg.sender.email_address, msg.subject, [a.name for a in msg.attachments])
With either import line, this prints the same report on my test tenant.
What it covers: folders and folder paths, querysets with every lookup, Q, order_by(), only(), values() and slices, messages (send, reply, forward, move, attachments of any size, MIME, headers), calendar items including recurring series and meeting responses, contacts, tasks (on Microsoft To Do), resolve_names(), expand_dl(), inbox rules, automatic replies, extended properties, bulk operations (on Graph $batch) and sync_items() / sync_hierarchy() (on Graph delta queries).
Two things I cared about most:
- Ids stay the same. Graph's ids of messages, events, contacts, folders and attachments are EWS's ids in another base64 alphabet, so ewsgraph returns them in exchangelib's form. Ids you stored keep working. (Calendar ids are the exception.)
-
No silent
None. If Graph has no answer for a field or a call, ewsgraph raisesUnsupportedFeatureorUnsupportedFieldbefore sending anything, with the reason and what to use instead.
What you still have to change
-
The app registration. EWS used
full_access_as_app. Graph needs its own application permissions:Mail.ReadWriteandMail.Send, plusCalendars.ReadWrite,Contacts.ReadWriteand so on for what you use. This command signs in and tells you which ones are missing:
ewsgraph doctor --tenant-id ... --client-id ... --mailbox ...
-
Username and password sign-in. Basic auth is already off in Exchange Online; use
OAuth2Credentials. - Access limited to some mailboxes. If you used an Application Access Policy, its replacement is RBAC for Applications in Exchange Online.
What does not work, and never will
Graph has no API for public folders, the archive mailbox, item export and upload, pull and streaming subscriptions or personal distribution lists, so ewsgraph cannot offer them either. It also does not talk to Exchange servers on premises; keep exchangelib there.
There are smaller differences too. Query strings go through Graph's $search (at most 1,000 results). Graph compares text without case, so ewsgraph checks case itself where exchangelib does. Creating an event with attendees mails them at once. The README lists all of them.
How I checked it
- The same reads through exchangelib over EWS and through ewsgraph over Graph, on a Microsoft 365 test tenant with EWS still allowed, compared field by field: messages, HTML and text bodies, attachments by SHA-256, headers, ids, every occurrence of recurring events, free/busy, contacts, folders, rules, tasks (64 steps).
- 46 public projects that use exchangelib with OAuth: 35 of the 45 I could check use only what ewsgraph has, with the import changed.
- 1,390+ offline tests on Python 3.10 to 3.13.
Trying it
pip install ewsgraph
Disclosure, since I wrote it: ewsgraph is source-available, not open source. It is free for personal and noncommercial use and for open-source projects that depend on it. Other business use needs a license ($99 for one application, $1,490 for every application in a tenant), and the library keeps working without one; it only prints a warning. Details are on the product page. It is not affiliated with Microsoft or the exchangelib project.
If your exchangelib code uses something ewsgraph does not cover, please open an issue. That is the most useful thing you can tell me before April.
Top comments (0)