如何开始使用Python的FOSS
aspose-email-foss for Python 是一个免费的 MIT 许可证库,用于创建、阅读和使用。 转换 Outlook MSG 文件和 CFB 容器 - 不需要 Microsoft Office,不需要原生 扩展,纯Python从PYPI。.
步骤指南
步骤 1:安装软件包
使用Pip的PyPI安装:
pip install aspose-email-foss检查安装正确的装载:
from aspose.email_foss.msg import MapiMessage
print("aspose-email-foss is ready.")步骤2:进口所需的类别
导入您需要创建消息、分配属性和格式转换的模块:
from datetime import datetime, timezone
from aspose.email_foss import msg
from aspose.email_foss.msg import MapiMessage, PropertyId步骤三:创建一个文件
使用 MapiMessage.create() 用主题和身体构建一个消息。 与 PropertyId 常数,添加接收者,并附上文件。.
message = MapiMessage.create("Getting Started", "Hello from aspose-email-foss!")
message.set_property(PropertyId.SENDER_NAME, "Build Agent")
message.set_property(PropertyId.SENDER_EMAIL_ADDRESS, "agent@example.com")
message.set_property(
PropertyId.MESSAGE_DELIVERY_TIME,
datetime(2026, 1, 1, 9, 0, tzinfo=timezone.utc),
)
message.add_recipient("alice@example.com", display_name="Alice Example")
message.add_attachment("notes.txt", b"Meeting notes go here.\n", mime_type="text/plain")
message.save("getting-started.msg")
print("getting-started.msg written.")步骤 4:阅读 MSG 文件返回
使用保存的文件加载 MapiMessage.from_file() 作为一个环境管理员,访问 subject 和 body 直接从返回的物体。.
with MapiMessage.from_file("getting-started.msg") as message:
print(f"Subject: {message.subject}")
print(f"Body : {message.body}")第五步:将你的位置转换为“空中”
to_email_bytes() 将已加载的消息转换为 RFC 5322 格式. 写下结果到 阿 .eml 使用标准图书馆的文件。.
from pathlib import Path
with MapiMessage.from_file("getting-started.msg") as message:
Path("getting-started.eml").write_bytes(message.to_email_bytes())
print("getting-started.eml written.")常见问题与解决方案
ModuleNotFoundError: No module named 'aspose' 该包未安装在活跃的 Python 环境中。 pip install aspose-email-foss 在您正在使用的相同虚拟环境中运行脚本。 pip show aspose-email-foss.
FileNotFoundError 当打电话时 MapiMessage.from_file() 您通过的路径不存在或使用错误的分离器为您的操作系统。 pathlib.Path 以便在移动方式建造路径: MapiMessage.from_file(Path("folder") / "file.msg").
MSG 文件在 Outlook 中打开,但没有显示主题 MapiMessage.create() 接受主題作為第一個位置論點. 如果您通過了 一个空串,Outlook将消息显示为未标题。 明确与 set_property(PropertyId.SUBJECT, "My Subject").
EML 转换产生一个空体 to_email_bytes() 使用 body_html 如果设置,并返回 body.确认在 至少其中一个属性在加载 MSG 后不空。 from_file().
收件人或主题中的 Unicode 内容被剪切 通行程 unicode_strings=True 两 MapiMessage.create() 确保 string 的属性是 存储在 Unicode (PT_UNICODE) 而不是 ANSI ( PT_STRING8) 中。.
常见问题
是否需要 Microsoft Office?
它读取和写入二进制 MSG 和 CFB 格式的 Python 中原生,而不依赖于 Microsoft Office、COM 自动化或 Win32 API。.
我可以在 Linux 或 macOS 上使用此库吗?
是的。. aspose-email-foss 它是纯粹的Python,可以在任何支持PYTHON 3.10的平台上运行。 或更高版本,包括 Windows、macOS、Linux、Docker容器和无服务器功能。.
图书馆是否免费用于商业用途?
是的,它是在MIT许可证下发布的;您可以使用、修改和再分发用于任何目的,包括商业应用程序,而不需要超过许诺文本的授权费或归属要求。.
我如何添加 CC 和 BCC 接收器?
通行程 recipient_type=msg.RECIPIENT_TYPE_CC 或 msg.RECIPIENT_TYPE_BCC 作为第三个 论点2 add_recipient():
message.add_recipient("bob@example.com", display_name="Bob", recipient_type=msg.RECIPIENT_TYPE_CC)我如何将一个 MSG 嵌入另一个?
使用 MapiMessage.add_embedded_message_attachment(),通过内在的 MapiMessage 作为第一个论点:
inner = MapiMessage.create("Inner message", "Embedded body.")
outer = MapiMessage.create("Outer message", "See attachment.")
outer.add_embedded_message_attachment(inner, "inner.msg")
outer.save("outer.msg")