Skip to content

OutlookService

Send emails, list messages, retrieve message details, and download attachments via the Microsoft Graph API.

Credentials

Uses the Entra ID app registration (tenant_id, client_id, client_secret). Typical Graph permissions: Mail.Send, Mail.Read.

Usage

python
from elsai_cloud.outlook import OutlookService

outlook_service = OutlookService(
    tenant_id="your_tenant_id",
    client_id="your_client_id",
    client_secret="your_client_secret",
)

Constructor parameters:

ParameterRequiredDescription
tenant_idYesAzure AD tenant ID
client_idYesAzure AD app client ID
client_secretYesAzure AD app client secret

Send a plain-text email:

python
result = outlook_service.send_email(
    sender_email="sender@example.com",
    to_recipients=["recipient@example.com"],
    subject="Test Email",
    body="This is a test email.",
    cc_recipients=["cc@example.com"],
    is_html=False,
)
print("Result:", result)

Send an email with attachments:

python
result = outlook_service.send_email(
    sender_email="sender@example.com",
    to_recipients=["recipient@example.com"],
    subject="Email with Attachments",
    body="Please find the attached files.",
    attachments=["/path/to/file.pdf"],
    is_html=False,
)

Send an HTML email:

python
html_body = """
<html><body>
    <h1>Welcome!</h1>
    <p>This is an <strong>HTML formatted</strong> email.</p>
</body></html>
"""

result = outlook_service.send_email(
    sender_email="sender@example.com",
    to_recipients=["recipient@example.com"],
    subject="HTML Email",
    body=html_body,
    is_html=True,
)

send_email parameters:

ParameterRequiredDescription
sender_emailYesSender's email address
to_recipientsYesList of recipient email addresses
subjectYesEmail subject
bodyYesEmail body (plain text or HTML)
cc_recipientsNoList of CC email addresses
attachmentsNoList of local file paths to attach
is_htmlNoSet True to send body as HTML (default: False)

List messages:

python
emails = outlook_service.list_messages(
    user_email="user@example.com",
    top=25,
    query="Invoices",   # optional filter keyword
)

if emails.get("value"):
    print(f"Found {len(emails['value'])} emails")

Get a specific message:

python
message = outlook_service.get_message(
    user_email="user@example.com",
    message_id="message_id_here",
)
print(f"Subject: {message.get('subject')}")

Get a message with attachment status:

python
msg = outlook_service.get_message_with_attachments(
    user_email="user@example.com",
    message_id="message_id_here",
)
print(f"Has attachments: {msg['has_attachments']}")

List and download attachments:

python
attachments = outlook_service.list_attachments(
    user_email="user@example.com",
    message_id="message_id_here",
)

if attachments.get("value"):
    att = attachments["value"][0]
    saved_path = outlook_service.download_attachment(
        user_email="user@example.com",
        message_id="message_id_here",
        attachment_id=att["id"],
        attachment_name=att["name"],
        download_dir="./downloads",
    )
    print(f"Saved to: {saved_path}")

See also

Copyright © 2026 elsai foundry.