Quickstart

快速启动:创建和阅读MSG文件

这个指南将您从一个新安装到工作的MSG文件,使用几步 MapiMessage 于 年 月 日 aspose.email_foss.


Prerequisites

RequirementDetail
Python3.10 or later
当前版本
OSWindows,macOS或Linux的版本
Dependencies没有纯Python

步骤1 安装包

pip install aspose-email-foss

确认安装:

from aspose.email_foss.msg import MapiMessage
print("aspose-email-foss is ready.")

步骤2 创建一个MSG文件

使用情况 MapiMessage.create() 建立一个消息,设置发送者属性与 PropertyId, 添加接收者,附加文件并调用 save() 写出一个字母. .msg 输出.

from datetime import datetime, timezone
from aspose.email_foss import msg

message = msg.MapiMessage.create("Hello from Python", "This is the message body.")
message.set_property(msg.PropertyId.SENDER_NAME, "Build Agent")
message.set_property(msg.PropertyId.SENDER_EMAIL_ADDRESS, "agent@example.com")
message.set_property(
    msg.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("readme.txt", b"Hello, world!\n", mime_type="text/plain")
message.save("quickstart.msg")
print("quickstart.msg written.")

步骤3 重新阅读MSG文件

使用下载文件加载保存的文件. MapiMessage.from_file() 并且访问 subject 及其他 body 直接的财产.

from aspose.email_foss import msg

with msg.MapiMessage.from_file("quickstart.msg") as message:
    print(f"Subject : {message.subject}")
    print(f"Body    : {message.body}")

第4步 转换为EML

to_email_message() 返回一个标准的 Python email.message.EmailMessage 目标.使用 to_email_bytes() 编写一个准备发送的 RFC 5322 .eml 文件.

from pathlib import Path
from aspose.email_foss import msg

with msg.MapiMessage.from_file("quickstart.msg") as message:
    Path("quickstart.eml").write_bytes(message.to_email_bytes())
    print("quickstart.eml written.")

下一步步骤

See Also

 中文