MapiMessage
Overview
MapiMessage is the primary entry point for working with Outlook MSG files in Aspose.Email for Python. It supports creating new messages from scratch, loading existing .msg files, modifying recipients, attachments, and arbitrary MAPI properties, and serializing to MSG or EML formats. Use it as a context manager to ensure the underlying MsgReader is closed after use.
from datetime import datetime, timezone
from aspose.email_foss import msg
message = msg.MapiMessage.create("Hello", "Body")
message.set_property(msg.PropertyId.SENDER_NAME, "Build Agent")
message.set_property(msg.PropertyId.SENDER_EMAIL_ADDRESS, "build.agent@example.com")
message.set_property(msg.PropertyId.MESSAGE_DELIVERY_TIME, datetime(2026, 3, 15, 10, 30, tzinfo=timezone.utc))
message.add_recipient("alice@example.com", display_name="Alice Example")
message.add_attachment("hello.txt", b"sample attachment\n", mime_type="text/plain")
message.save("example-message.msg")
with msg.MapiMessage.from_file("example-message.msg") as loaded:
email_message = loaded.to_email_message()
print(email_message["Subject"])Class Methods
| Signature | Description |
|---|---|
from_file(path, strict=False) -> MapiMessage | Load from a .msg file path. |
from_msg_document(document) -> MapiMessage | Construct from a low-level MsgDocument. |
from_email_message(email_message, unicode_strings=True) -> MapiMessage | Convert from a Python email.message.EmailMessage. |
create(subject, body, unicode_strings=True) -> MapiMessage | Create a new blank message with the given subject and plain-text body. |
Properties
| Name | Type | Access | Description |
|---|---|---|---|
msg_reader | MsgReader | read-only | Underlying MsgReader instance. |
validation_issues | tuple[str, ...] | read-only | Non-fatal parse issues encountered during load. |
subject | str | None | read/write | Message subject. |
body | str | None | read/write | Plain-text body. |
message_class | str | None | read/write | MAPI message class string (e.g. IPM.Note). |
body_html | str | None | read/write | HTML body. |
Methods
| Signature | Description |
|---|---|
save(path) -> None | Write the message to a .msg file at path. |
to_bytes() -> bytes | Serialize the message to in-memory MSG bytes. |
to_email_message() -> EmailMessage | Convert to a Python email.message.EmailMessage. |
to_email_bytes() -> bytes | Serialize as EML-format bytes. |
to_email_string() -> str | Serialize as an EML-format string. |
to_msg_document() -> MsgDocument | Convert to a low-level MsgDocument. |
close() -> None | Release underlying MsgReader resources. |
add_recipient(email_address, display_name=None, recipient_type=1) -> MapiRecipient | Append a recipient; recipient_type 1=TO, 2=CC, 3=BCC. |
add_attachment(filename, data, mime_type=None, content_id=None) -> MapiAttachment | Append a file attachment from bytes. |
add_embedded_message_attachment(message, filename=None, mime_type=None) -> MapiAttachment | Embed another MapiMessage as an attachment. |
iter_properties() -> Iterator[MapiProperty] | Iterate all top-level MAPI properties. |
iter_attachments_info() -> Iterator[MapiAttachment] | Iterate attachment metadata objects. |
set_property(property_id, property_type_or_value, value=..., flags=...) -> MapiProperty | Set or replace a MAPI property. |
get_property(property_id, property_type=None, storage_stream_id=0, decode=True) -> MapiProperty | Any | None | Retrieve a MapiProperty by ID. |
get_property_value(property_id, property_type=None, storage_stream_id=0, decode=True) -> Any | None | Retrieve only the decoded value of a property. |
set_named_property(named_property, property_type, value, flags=...) -> MapiProperty | Set a named (string or LID) MAPI property. |
get_named_property(named_property, property_type=None) -> MapiProperty | None | Retrieve a named MAPI property. |