Exchangelib

5.6.0 · active · verified Sat Apr 11

Exchangelib is a Python client for Microsoft Exchange Web Services (EWS), providing programmatic access to Exchange mailboxes, calendars, contacts, and tasks. It supports autodiscovery, various authentication methods (NTLM, OAuth), and aims to be a comprehensive, easy-to-use interface for EWS. The current version is 5.6.0, with a release cadence that responds to bug fixes, feature requests, and EWS changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to an Exchange mailbox using email/password credentials and autodiscovery. It then fetches and prints the subjects of the 5 most recent unread items in the inbox. It uses environment variables for security to avoid hardcoding sensitive information.

import os
from exchangelib import Account, Credentials, Configuration, DELEGATE

# Get credentials from environment variables for security
email = os.environ.get('EXCHANGE_EMAIL', 'your_email@example.com')
password = os.environ.get('EXCHANGE_PASSWORD', 'your_password')

if not email or not password or email == 'your_email@example.com':
    print("Please set EXCHANGE_EMAIL and EXCHANGE_PASSWORD environment variables.")
    print("Or replace placeholders directly in the script (not recommended for production).")
    exit(1)

credentials = Credentials(email, password)

try:
    # Autodiscover the EWS URL and connect
    account = Account(
        primary_smtp_address=email,
        credentials=credentials,
        autodiscover=True,
        access_type=DELEGATE # Or IMPERSONATION, ARCHIVE, etc.
    )
    print(f"Successfully connected to Exchange for {account.primary_smtp_address}")
    print(f"EWS URL: {account.protocol.ews_url}")

    # Example: List subjects of 5 most recent unread items in Inbox
    inbox = account.inbox
    print(f"\nListing up to 5 unread items in Inbox for {email}:")
    for item in inbox.filter(is_read=False).order_by('-datetime_received')[:5]:
        print(f"- Subject: {item.subject}, From: {item.sender.email_address}, Received: {item.datetime_received}")

except Exception as e:
    print(f"Could not connect or perform operation: {e}")
    print("Common issues: Incorrect credentials, firewall blocking EWS, autodiscovery failure.")
    print("If autodiscovery fails, try setting `autodiscover=False` and `ews_url` manually in Configuration.")

view raw JSON →