简介
以编程方式解析电子邮件消息是数据迁移、归档工作流和合规工具的常见需求。Outlook MSG 文件使用复合文件二进制(CFB)容器格式和 MAPI 属性来编码消息元数据、正文内容、收件人和附件。在 Python 中处理此格式传统上需要商业库或低层二进制操作。
The aspose-email-foss 包提供了一个纯 Python、MIT 许可的库,用于读取、创建和转换 Outlook MSG 文件。它公开了一个用于常见任务的高级 MapiMessage API,以及用于对底层 CFB 结构进行详细检查的低层 MsgReader 访问。该库要求 Python 3.10 或更高版本,并且没有本地依赖。
无论您是需要从一批归档消息中提取主题行和正文、枚举附件以进行索引,还是将 MSG 文件转换为标准 RFC 5322 格式,该库都会处理二进制解析,让您专注于应用逻辑。
包含的内容
使用 MapiMessage 读取 MSG 文件
MapiMessage 类是解析 Outlook MSG 文件的主要入口。打开文件,访问其属性,并使用上下文管理器干净地关闭它。
from aspose.email_foss import msg
with msg.MapiMessage.from_file("meeting-invite.msg") as message:
print(f"Subject: {message.subject}")
print(f"Body: {message.body[:200]}")
print(f"HTML body available: {message.body_html is not None}")
该 from_file 方法读取 CFB 容器,解析 MAPI 属性流,并填充已类型化的属性。subject、body 和 body_html 属性直接返回解码后的字符串。
提取收件人
每个 MSG 文件在专用的 MAPI 存储中保存收件人信息。MapiMessage.recipients 属性返回一个 MapiRecipient 对象列表,这些对象具有显示名称、电子邮件地址和收件人类型(TO、CC 或 BCC)的已类型化字段。
from aspose.email_foss import msg
with msg.MapiMessage.from_file("team-update.msg") as message:
for recipient in message.recipients:
print(
f"{recipient.display_name} <{recipient.email_address}> "
f"type={recipient.recipient_type}"
)
处理附件
该 iter_attachments_info 方法产生 MapiAttachment 对象,这些对象提供文件名、原始字节、MIME 类型,以及用于内联图像的可选 Content-ID。
from aspose.email_foss import msg
with msg.MapiMessage.from_file("report.msg") as message:
for att in message.iter_attachments_info():
print(f" {att.filename} ({att.mime_type}, {len(att.data)} bytes)")
if att.content_id:
print(f" inline content-id: {att.content_id}")
访问 MAPI 属性
对于需要超出便利属性的特定 MAPI 属性值的场景,请使用带有 PropertyId 常量的 get_property_value。这可直接访问 MSG 文件中存储的任何属性。
from aspose.email_foss import msg
with msg.MapiMessage.from_file("notification.msg") as message:
sender = message.get_property_value(msg.PropertyId.SENDER_NAME)
email = message.get_property_value(msg.PropertyId.SENDER_EMAIL_ADDRESS)
delivery = message.get_property_value(msg.PropertyId.MESSAGE_DELIVERY_TIME)
print(f"From: {sender} <{email}>")
print(f"Delivered: {delivery}")
遍历所有属性
当您需要检查消息中的每个属性(用于调试、审计或迁移)时,iter_properties 方法会产生带有完整标签和值信息的 MapiProperty 对象。
from aspose.email_foss import msg
with msg.MapiMessage.from_file("sample.msg") as message:
for prop in message.iter_properties():
print(
f"tag=0x{prop.property_tag:08X} "
f"type=0x{prop.property_type:04X} "
f"value={prop.value!r}"
)
将 MSG 转换为 EML
to_email_message 方法将解析后的 MSG 转换为标准的 Python email.message.EmailMessage 对象。随后,您可以使用标准库将消息序列化为 RFC 5322(EML)格式。
from aspose.email_foss import msg
with msg.MapiMessage.from_file("archive.msg") as message:
email_message = message.to_email_message()
eml_bytes = email_message.as_bytes()
with open("archive.eml", "wb") as f:
f.write(eml_bytes)
请注意,EML 输出使用 Python 内置的 email 模块进行序列化。该库处理 MSG 到 MAPI 再到 EmailMessage 的转换;标准库处理 RFC 5322 的传输格式。
快速入门
从 PyPI 安装软件包,并在不到 20 行代码中解析您的第一个 MSG 文件:
# Install: pip install aspose-email-foss>=26.3
from aspose.email_foss import msg
# Create a new message
message = msg.MapiMessage.create(
"Project status update",
"Hello team,\n\nPlease review the attached report.\n\nRegards",
)
message.add_recipient("alice@example.com", display_name="Alice")
message.add_attachment("notes.txt", b"meeting notes\n", mime_type="text/plain")
message.save("status-update.msg")
# Read it back and extract data
with msg.MapiMessage.from_file("status-update.msg") as loaded:
print(f"Subject: {loaded.subject}")
print(f"Recipients: {len(loaded.recipients)}")
for att in loaded.iter_attachments_info():
print(f"Attachment: {att.filename} ({len(att.data)} bytes)")
支持的格式
| 格式 | 扩展名 | 读取 | 写入 |
|---|---|---|---|
| Outlook MSG | .msg | 是 | 是 |
| 复合文件二进制 | .cfb | 是 | 是 |
| EML(通过转换) | .eml | 是(通过 from_email_message) | 是(通过 to_email_message) |
EML 支持通过转换实现:MapiMessage.from_email_message() 从 Python email.message.EmailMessage 对象导入,而 to_email_message() 再导出。未提供直接的 EML 文件解析——标准库处理 EML 的传输格式。
开源与许可
Aspose.Email FOSS for Python 在 MIT 许可证下发布。您可以自由在商业和非商业项目中使用、修改和分发该库。完整源码可在 GitHub 上获取。
该库是 pure-Python 实现,不含编译扩展或本地依赖。它可在任何支持 Python 3.10 或更高版本的平台上运行。
开始使用
探索更多关于使用 Aspose.Email FOSS 进行 Python 的资源:
- Developer Guide — 包含教程和 API 演练的完整文档
- Knowledge Base — 操作指南文章和常见问题
- API Reference — 完整的类和方法参考