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

SignatureDescription
from_file(path, strict=False) -> MapiMessageLoad from a .msg file path.
from_msg_document(document) -> MapiMessageConstruct from a low-level MsgDocument.
from_email_message(email_message, unicode_strings=True) -> MapiMessageConvert from a Python email.message.EmailMessage.
create(subject, body, unicode_strings=True) -> MapiMessageCreate a new blank message with the given subject and plain-text body.

Properties

NameTypeAccessDescription
msg_readerMsgReaderread-onlyUnderlying MsgReader instance.
validation_issuestuple[str, ...]read-onlyNon-fatal parse issues encountered during load.
subjectstr | Noneread/writeMessage subject.
bodystr | Noneread/writePlain-text body.
message_classstr | Noneread/writeMAPI message class string (e.g. IPM.Note).
body_htmlstr | Noneread/writeHTML body.

Methods

SignatureDescription
save(path) -> NoneWrite the message to a .msg file at path.
to_bytes() -> bytesSerialize the message to in-memory MSG bytes.
to_email_message() -> EmailMessageConvert to a Python email.message.EmailMessage.
to_email_bytes() -> bytesSerialize as EML-format bytes.
to_email_string() -> strSerialize as an EML-format string.
to_msg_document() -> MsgDocumentConvert to a low-level MsgDocument.
close() -> NoneRelease underlying MsgReader resources.
add_recipient(email_address, display_name=None, recipient_type=1) -> MapiRecipientAppend a recipient; recipient_type 1=TO, 2=CC, 3=BCC.
add_attachment(filename, data, mime_type=None, content_id=None) -> MapiAttachmentAppend a file attachment from bytes.
add_embedded_message_attachment(message, filename=None, mime_type=None) -> MapiAttachmentEmbed 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=...) -> MapiPropertySet or replace a MAPI property.
get_property(property_id, property_type=None, storage_stream_id=0, decode=True) -> MapiProperty | Any | NoneRetrieve a MapiProperty by ID.
get_property_value(property_id, property_type=None, storage_stream_id=0, decode=True) -> Any | NoneRetrieve only the decoded value of a property.
set_named_property(named_property, property_type, value, flags=...) -> MapiPropertySet a named (string or LID) MAPI property.
get_named_property(named_property, property_type=None) -> MapiProperty | NoneRetrieve a named MAPI property.

See Also