功能与特性

功能与特性

本页涵盖了每个主要特性 aspose-email-foss 26.3 提供可运行的 Python 示例。.


读取 MSG 文件

加载 MSG 文件并访问其核心属性::

from aspose.email_foss.msg import MapiMessage

msg = MapiMessage.from_file("message.msg")

print(f"Subject: {msg.subject}")
print(f"Body: {msg.body}")
print(f"HTML Body: {msg.body_html}")
print(f"Message Class: {msg.message_class}")

创建 MSG 文件

从头创建新消息::

from aspose.email_foss.msg import MapiMessage

msg = MapiMessage.create("Meeting Notes", "Please find the notes attached.")
msg.save("new_message.msg")

在现有消息上设置主题和正文::

msg.subject = "Updated Subject"
msg.body = "Updated body text"
msg.save("updated.msg")

收件人

添加带有显示名称和类型的收件人::

from aspose.email_foss.msg import MapiMessage, RECIPIENT_TYPE_CC

msg = MapiMessage.create("Hello", "Message body")
msg.add_recipient("alice@example.com", display_name="Alice Smith")
msg.add_recipient("bob@example.com", display_name="Bob Jones", recipient_type=RECIPIENT_TYPE_CC)
msg.save("with_recipients.msg")

附件

添加二进制文件附件::

with open("report.pdf", "rb") as f:
    data = f.read()

msg.add_attachment("report.pdf", data, mime_type="application/pdf")
msg.save("with_attachment.msg")

将 MSG 消息嵌入为嵌套附件::

inner = MapiMessage.create("Inner Subject", "Inner body")
msg.add_embedded_message_attachment(inner, filename="embedded.msg", mime_type="application/vnd.ms-outlook")
msg.save("with_embedded.msg")

MAPI 属性

按属性 ID 读取和写入 MAPI 属性::

from aspose.email_foss.msg import PropertyId

# Read a property
subject = msg.get_property(PropertyId.SUBJECT)

# Set a property
msg.set_property(PropertyId.SUBJECT, "New Subject")

遍历所有属性::

for prop in msg.iter_properties():
    print(f"Tag: {prop.property_tag:#010x}")

电子邮件转换

在 MSG(MAPI)和电子邮件(RFC 5322)格式之间转换::

# MSG to email string
email_str = msg.to_email_string()
print(email_str[:500])

# MSG to EmailMessage object
email_msg = msg.to_email_message()

# EmailMessage back to MapiMessage
msg2 = MapiMessage.from_email_message(email_msg)
msg2.save("roundtrip.msg")

低层 CFB 操作

直接读取 CFB 容器结构::

from aspose.email_foss.cfb import CFBReader

reader = CFBReader.from_file("message.msg")
print(f"File size: {reader.file_size} bytes")
print(f"Sectors: {reader.fat_sector_count}")
print(f"Entries: {reader.directory_entry_count}")

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

for entry in reader.iter_streams():
    print(f"Stream: {entry}")

reader.close()

写入 CFB 文档::

from aspose.email_foss.msg import MsgWriter

data = msg.to_bytes()
with open("output.msg", "wb") as f:
    f.write(data)

技巧与最佳实践

  • 始终调用 msg.close() 或在读取完成后使用上下文管理器以释放文件句柄
  • 使用 msg.validation_issues 检查 MSG 格式合规性问题
  • strict 参数在 MapiMessage.from_file() 控制错误容忍度 — 设置为 False 以进行宽松解析
  • 所有附件数据在 from_file() — 使用 att.data 直接访问字节;; iter_attachments_info() 是已填充附件列表的便利迭代器
  • 使用 to_email_string() 用于快速文本预览,而无需完整的电子邮件对象构建

常见问题

问题原因修复
CFBError 加载时不是有效的 CFB/MSG 文件确认文件是实际的 Outlook MSG
加载后正文为空正文仅以 HTML 形式存储检查 msg.body_html 而不是 msg.body
缺少收件人MSG 没有收件人存储在 MSG 格式中收件人是可选的;请检查 iter_attachments_info()

FAQ

支持哪些 MSG 格式版本??

该库读取和写入 CFB v3 与 v4 格式的 MSG 文件,兼容 Microsoft Outlook。.

我可以读取 EML 文件吗??

不能直接。该库处理 MSG 格式。使用 MapiMessage.from_email_message() 将 RFC 5322 转换为 EmailMessage 对象。.

是否支持线程安全??

每个 MapiMessageCFBReader 实例是独立的。对不同线程中的独立实例进行并发访问是安全的。.


API 参考摘要

类 / 方法描述
MapiMessage高级 MSG 消息表示
MapiMessage.from_file()加载 MSG 文件
MapiMessage.create()创建新的 MSG 消息
MapiMessage.save()保存为 MSG 文件
MapiMessage.to_email_message()转换为 EmailMessage(RFC 5322)
MapiMessage.from_email_message()从 EmailMessage 转换为 MSG
MapiMessage.add_recipient()添加收件人
MapiMessage.add_attachment()添加二进制附件
MapiAttachment消息的附件
MapiRecipient消息的收件人
MapiPropertyMAPI 属性条目
CFBReader低层 CFB 容器读取器
CFBWriter低层 CFB 容器写入器
MsgReader低层 MSG 结构读取器
 中文