简介

Aspose.Email FOSS for Python is now available on PyPI: a free, MIT-licensed library for creating, reading, and converting Outlook .msg 文件完全采用 Python,不依赖 Microsoft Office 或任何本机扩展。使用以下方式安装它 pip install aspose-email-foss>=26.3 并立即开始使用 MSG 和 CFB 容器进行工作。.

该库面向 Python 3.10 及更高版本。它从头实现了复合文件二进制(CFB)格式和 MSG 消息格式,让您对电子邮件的序列化和读取拥有确定性的控制。由于实现纯粹基于 Python,它在 Windows、macOS、Linux 以及容器化环境中运行完全相同。.

Aspose.Email FOSS is designed for developers building email processing pipelines, compliance archival tools, forensic analysis workflows, or any application that needs to create or inspect Outlook-compatible message files without a running Exchange or Outlook installation.


关键特性

创建 MSG 文件

构建兼容 Outlook 的 .msg 文件从头开始使用 MapiMessage.create(). 通过 设置标准 MAPI 属性,如主题、发件人、投递时间和显示字段 PropertyId enum. 添加收件人使用 add_recipient() 以及文件附件使用 add_attachment(), 然后将结果保存到磁盘。.

from aspose.email_foss import msg

message = msg.MapiMessage.create(
    "Quarterly status update and rollout plan",
    "Hello team,\n\nPlease find the latest rollout summary attached.\n\nRegards,\nEngineering",
)

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.INTERNET_MESSAGE_ID, "<example-001@example.com>")

message.add_recipient("alice@example.com", display_name="Alice Example")
message.add_recipient("bob@example.com", display_name="Bob Example")
message.add_recipient(
    "carol@example.com",
    display_name="Carol Example",
    recipient_type=msg.RECIPIENT_TYPE_CC,
)

message.add_attachment("hello.txt", b"sample attachment\n", mime_type="text/plain")
message.save("example-message.msg")

读取并将 MSG 转换为 EML

加载现有的 .msg 文件使用 MapiMessage.from_file() 并将其转换为标准的 Python EmailMessage 对象通过 to_email_message(). 从那里,将其序列化为 EML 字节,以便存储或通过 SMTP 转发。.

from aspose.email_foss import msg

with msg.MapiMessage.from_file("example-message.msg") as loaded:
    email_message = loaded.to_email_message()
    eml_bytes = email_message.as_bytes()

with open("example-message.eml", "wb") as f:
    f.write(eml_bytes)

检查 MSG 内部结构

使用 MsgReader 及其底层 CFBReader 检查 MSG 文件的二进制结构。访问 CFB 元数据(版本、扇区大小、目录条目计数),并在二进制层面遍历 MAPI 属性条目。.

from aspose.email_foss import msg

reader = msg.MsgReader.from_file("example-message.msg")
cfb = reader.cfb_reader

print(f"CFB major_version={cfb.major_version}")
print(f"sector_size={cfb.sector_size}")
print(f"directory_entries={cfb.directory_entry_count}")

for entry in reader.iter_top_level_fixed_length_properties():
    tag = entry.property_tag
    print(f"tag=0x{tag:08X} flags=0x{entry.flags:08X} value={entry.value.hex()}")

reader.close()

低层 CFB 访问

读取并遍历任何复合文件二进制容器,使用 CFBReader. 枚举存储和流,按名称解析路径,并提取原始流数据以进行自定义处理。.

from aspose.email_foss.cfb import CFBReader

reader = CFBReader.from_file("example-message.msg")

for entry in reader.iter_storages():
    print(f"Storage: {entry.name}")

for entry in reader.iter_streams():
    data = reader.get_stream_data(entry.stream_id)
    print(f"Stream: {entry.name} size={len(data)}")

reader.close()

快速入门

安装库并在不到十行代码中创建您的第一个 MSG 文件::

pip install aspose-email-foss>=26.3
from aspose.email_foss import msg

message = msg.MapiMessage.create("Hello from Python", "This is a test message.")
message.set_property(msg.PropertyId.SENDER_EMAIL_ADDRESS, "sender@example.com")
message.add_recipient("recipient@example.com", display_name="Recipient")
message.save("hello.msg")

with msg.MapiMessage.from_file("hello.msg") as loaded:
    eml = loaded.to_email_message()
    print(eml["Subject"])

支持的格式

格式导入导出
MSG
CFB

开源与许可

Aspose.Email FOSS for Python is released under the MIT 许可证. 您可以在个人、内部和商业项目中无限制地使用它。源代码可在 GitHub.


入门指南