Quickstart

Quickstart: Create and Read an MSG File

This guide takes you from a fresh install to a working MSG file in a few steps using MapiMessage from aspose.email_foss.


Prerequisites

RequirementDetail
Python3.10 or later
pipcurrent version
OSWindows, macOS, or Linux
DependenciesNone — pure Python

Step 1 — Install the Package

pip install aspose-email-foss

Confirm the install:

from aspose.email_foss.msg import MapiMessage
print("aspose-email-foss is ready.")

Step 2 — Create an MSG File

Use MapiMessage.create() to build a message, set sender properties with PropertyId, add a recipient, attach a file, and call save() to write the .msg output.

from datetime import datetime, timezone
from aspose.email_foss import msg

message = msg.MapiMessage.create("Hello from Python", "This is the message body.")
message.set_property(msg.PropertyId.SENDER_NAME, "Build Agent")
message.set_property(msg.PropertyId.SENDER_EMAIL_ADDRESS, "agent@example.com")
message.set_property(
    msg.PropertyId.MESSAGE_DELIVERY_TIME,
    datetime(2026, 1, 1, 9, 0, tzinfo=timezone.utc),
)
message.add_recipient("alice@example.com", display_name="Alice Example")
message.add_attachment("readme.txt", b"Hello, world!\n", mime_type="text/plain")
message.save("quickstart.msg")
print("quickstart.msg written.")

Step 3 — Read the MSG File Back

Load the saved file with MapiMessage.from_file() and access the subject and body properties directly.

from aspose.email_foss import msg

with msg.MapiMessage.from_file("quickstart.msg") as message:
    print(f"Subject : {message.subject}")
    print(f"Body    : {message.body}")

Step 4 — Convert to EML

to_email_message() returns a standard Python email.message.EmailMessage object. Use to_email_bytes() to write a ready-to-send RFC 5322 .eml file.

from pathlib import Path
from aspose.email_foss import msg

with msg.MapiMessage.from_file("quickstart.msg") as message:
    Path("quickstart.eml").write_bytes(message.to_email_bytes())
    print("quickstart.eml written.")

Next Steps