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
| Requirement | Detail |
|---|---|
| Python | 3.10 or later |
| Pip | Phiên bản hiện tại |
| OS | Windows, macOS hoặc Linux |
| Dependencies | Không có - Python tinh khiết |
Bước 1 - Cài đặt gói
pip install aspose-email-fossChứ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 subject và body 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
- Hướng dẫn cài đặt: Thiết lập môi trường ảo, tùy chọn pip và xác minh
- Bắt đầu xem xét tổng quan: Tổng hợp các yêu cầu và tổng quan khả năng
- Chức năng và chức năng: Deep-dive vào mỗi tính năng
- Tham chiếu API