介绍
Aspose.Email FOSS 用于 Python(aspose-email-foss)是一个基于 MIT 许可证的 Python 库,用于处理 Outlook MSG 文件、RFC-5322 EML 消息以及复合文件二进制(CFB)存储结构。该库不需要本机扩展,可直接从 PyPI 安装。
该库弥补了常见的 Python 开发缺口:在无需运行 Outlook 或专有 SDK 的情况下读取和写入 Outlook .msg 文件。它还公开了支撑 .msg 文件的完整 CFB 容器格式,使其适用于从消息迁移到存储检查等各种任务。
该库提供了一个类型化的 API 接口,涵盖 MAPI 消息构建、收件人和附件管理、属性序列化以及 CFB 文档的往返处理。
关键特性
创建和序列化 MSG 文件
MapiMessage.create() 从主题和正文字符串构建一个新的 MAPI 消息。随后您可以添加类型化属性、收件人和二进制附件,然后使用 to_bytes() 将结果序列化为字节。
import datetime
from aspose.email_foss.msg.mapi_message import MapiMessage, PropertyId
message = MapiMessage.create("Quarterly Report", "Please review the attached report.")
message.set_property(PropertyId.SENDER_NAME, "Alice")
message.set_property(PropertyId.SENDER_EMAIL_ADDRESS, "alice@example.com")
message.set_property(
PropertyId.MESSAGE_DELIVERY_TIME,
datetime.datetime(2026, 3, 15, 10, 30, tzinfo=datetime.timezone.utc),
)
message.add_recipient("bob@example.com", display_name="Bob")
message.add_attachment("report.pdf", b"%PDF-1.4 ...", mime_type="application/pdf")
raw_bytes = message.to_bytes()
读取现有 MSG 文件
MapiMessage.from_file() 从磁盘读取现有的 .msg 文件。您也可以通过将 CFBReader 传递给 MsgReader 和 MsgDocument 来从原始字节重建消息。
from aspose.email_foss.msg.mapi_message import MapiMessage
from aspose.email_foss.msg.reader import MsgReader, MsgDocument
from aspose.email_foss.cfb.reader import CFBReader
# Read from file path
msg = MapiMessage.from_file("message.msg")
print(msg.subject)
# Or reconstruct from bytes
msg2 = MapiMessage.from_msg_document(
MsgDocument.from_reader(MsgReader(CFBReader(raw_bytes)))
)
print(msg2.subject)
for att in msg2.attachments:
print(att.filename, len(att.data), "bytes")
在 MAPI 与 RFC-5322 EML 之间转换
MapiMessage.from_email_message() 将标准的 Python email.message.EmailMessage 对象转换为 MAPI 消息,保留主题、收件人、正文和附件。反向转换使用 to_email_message()。
from email.message import EmailMessage
from aspose.email_foss.msg.mapi_message import MapiMessage
# Build a standard Python email
eml = EmailMessage()
eml["Subject"] = "Meeting notes"
eml["From"] = "alice@example.com"
eml["To"] = "bob@example.com"
eml.set_content("Notes from today's meeting.")
# Convert to MAPI and serialize as .msg
mapi = MapiMessage.from_email_message(eml)
with open("meeting-notes.msg", "wb") as f:
f.write(mapi.to_bytes())
# Round-trip back to EML
recovered = mapi.to_email_message()
print(recovered["Subject"])
复合文件二进制(CFB)存储
该库包含一个独立的 CFB 层(CFBReader、CFBWriter、CFBDocument、CFBStorage、CFBStream),用于在不依赖 MAPI 的情况下读取和写入复合文件二进制格式。这对于检查 .msg 文件的原始存储结构或构建自定义复合文件容器非常有用。
from aspose.email_foss.cfb.writer import CFBWriter, CFBDocument, CFBStorage, CFBStream
from aspose.email_foss.cfb.reader import CFBReader
# Build a CFB document with nested storage
root = CFBStorage("Root Entry")
root.add_stream(CFBStream("Metadata", b"meta-payload"))
nested = root.add_storage(CFBStorage("Attachments"))
nested.add_stream(CFBStream("Attachment1", b"binary-content"))
doc = CFBDocument(root=root, major_version=3)
data = CFBWriter.to_bytes(doc)
# Read it back
reader = CFBReader(data)
entry = reader.resolve_path(["Attachments", "Attachment1"])
print(reader.get_stream_data(entry.stream_id))
MAPI 属性访问
iter_properties() 将消息上的所有 MAPI 属性作为 MapiProperty 对象返回。命名属性(PidLidTag 和 PidNameTag)可通过 MapiNamedProperty 访问。MapiPropertyCollection 提供类似字典的属性访问方式。
from aspose.email_foss.msg.mapi_message import MapiMessage
msg = MapiMessage.from_file("message.msg")
for prop in msg.iter_properties():
print(hex(prop.property_id), prop.property_type, repr(prop.value)[:60])
快速入门
pip install aspose-email-fossimport datetime
from aspose.email_foss.msg.mapi_message import MapiMessage, PropertyId
from aspose.email_foss.msg.reader import MsgReader, MsgDocument
from aspose.email_foss.cfb.reader import CFBReader
# Create a message
message = MapiMessage.create("Hello", "This is the body.")
message.set_property(PropertyId.SENDER_NAME, "Sender")
message.set_property(PropertyId.SENDER_EMAIL_ADDRESS, "sender@example.com")
message.add_recipient("recipient@example.com", display_name="Recipient")
message.add_attachment("note.txt", b"attachment content", mime_type="text/plain")
# Serialize and reload
raw = message.to_bytes()
reloaded = MapiMessage.from_msg_document(
MsgDocument.from_reader(MsgReader(CFBReader(raw)))
)
email = reloaded.to_email_message()
print("Subject:", email["Subject"])
print("From:", email["From"])
支持的格式
| 格式 | 扩展名 | 读取 | 写入 |
|---|---|---|---|
| 消息 | .msg | ✓ | ✓ |
| CFB | .cfb | ✓ | ✓ |
| eml | .eml | ✓ | ✓ |
MSG 文件通过 MapiMessage.from_file() 和 MsgReader 读取。EML 通过 from_email_message() / to_email_message() 转换桥访问。CFB 容器直接由 CFBReader 和 CFBWriter 处理。
开源与许可
Aspose.Email FOSS 用于 Python 在 MIT License 下发布。源代码可在 GitHub 获取,软件包通过 PyPI 以 aspose-email-foss 名称分发。MIT 许可允许在商业应用中使用,无需除包含许可证外的版税或署名要求。