Quickstart

Quickstart: สร้างและอ่านไฟล์ MSG

คู่มือนี้จะพาคุณจากการติดตั้งใหม่สู่ไฟล์ MSG ที่ทํางานในระยะสั้น ๆ โดยใช้ MapiMessage จาก aspose.email_foss.


Prerequisites

RequirementDetail
Python3.10 or later
pip ปิปรูปแบบปัจจุบัน
OSWindows, macOS หรือ Linux
Dependenciesไม่มี ไพธอนที่บริสุทธิ์

ขั้นตอน 1 ติดตั้งแพ็คเกจ

pip install aspose-email-foss

ยืนยันการติดตั้ง:

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

ขั้นตอน 2 สร้างไฟล์ MSG

การใช้งาน MapiMessage.create() เพื่อสร้างข้อความ, กําหนด Properties ผู้ส่งด้วย 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

 ภาษาไทย