Quickstart

Quickstart: Tạo và đọc một tệp MSG

Hướng dẫn này sẽ đưa bạn từ một cài đặt mới đến một tệp MSG hoạt động trong vài bước sử dụng. MapiMessage Từ aspose.email_foss.


Prerequisites

RequirementDetail
Python3.10 or later
PipPhiên bản hiện tại
OSWindows, macOS hoặc Linux
DependenciesKhông có - Python tinh khiết

Bước 1 - Cài đặt gói

pip install aspose-email-foss

Chứng nhận cài đặt:

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

Bước 2 - Tạo một tệp MSG

Sử dụng MapiMessage.create() để xây dựng một tin nhắn, thiết lập các thuộc tính gửi với PropertyId, Thêm một người nhận, thêm tệp và gọi save() để viết The .msg sản xuất .

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.")

Bước 3 - Đọc lại tệp MSG

tải file được lưu với MapiMessage.from_file() và truy cập vào subjectbody Tài sản trực tiếp.

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}")

Bước 4 - Chuyển sang EML

to_email_message() Lại trả lại Python tiêu chuẩn email.message.EmailMessage mục đích: sử dụng to_email_bytes() để viết một sẵn sàng gửi RFC 5322 .eml file .

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.")

Bước tiếp theo

See Also

 Tiếng Việt