Giới thiệu

Aspose.Email FOSS cho Python hiện đã có trên PyPI: một thư viện miễn phí, có giấy phép MIT, dùng để tạo, đọc và chuyển đổi các tệp Outlook .msg hoàn toàn bằng Python, không phụ thuộc vào Microsoft Office hay bất kỳ phần mở rộng gốc nào. Cài đặt nó bằng pip install aspose-email-foss>=26.3 và bắt đầu làm việc với các container MSG và CFB ngay lập tức.

Thư viện này nhắm tới Python 3.10 trở lên. Nó triển khai định dạng Compound File Binary (CFB) và định dạng tin nhắn MSG từ đầu, cung cấp cho bạn kiểm soát quyết định về cách các tin nhắn email được tuần tự hoá và đọc. Vì việc triển khai hoàn toàn bằng Python, nó chạy giống hệt trên Windows, macOS, Linux và trong môi trường container.

Aspose.Email FOSS được thiết kế cho các nhà phát triển xây dựng các pipeline xử lý email, công cụ lưu trữ tuân thủ, quy trình phân tích pháp y, hoặc bất kỳ ứng dụng nào cần tạo hoặc kiểm tra các tệp tin tin nhắn tương thích với Outlook mà không cần cài đặt Exchange hoặc Outlook đang chạy.


Tính năng chính

Tạo Tệp MSG

Xây dựng các tệp .msg tương thích Outlook từ đầu bằng cách sử dụng MapiMessage.create(). Đặt các thuộc tính MAPI tiêu chuẩn như tiêu đề, người gửi, thời gian giao và các trường hiển thị thông qua enum PropertyId. Thêm người nhận bằng add_recipient() và tệp đính kèm bằng add_attachment(), sau đó lưu kết quả vào đĩa.

from aspose.email_foss import msg

message = msg.MapiMessage.create(
    "Quarterly status update and rollout plan",
    "Hello team,\n\nPlease find the latest rollout summary attached.\n\nRegards,\nEngineering",
)

message.set_property(msg.PropertyId.SENDER_NAME, "Build Agent")
message.set_property(msg.PropertyId.SENDER_EMAIL_ADDRESS, "build.agent@example.com")
message.set_property(msg.PropertyId.INTERNET_MESSAGE_ID, "<example-001@example.com>")

message.add_recipient("alice@example.com", display_name="Alice Example")
message.add_recipient("bob@example.com", display_name="Bob Example")
message.add_recipient(
    "carol@example.com",
    display_name="Carol Example",
    recipient_type=msg.RECIPIENT_TYPE_CC,
)

message.add_attachment("hello.txt", b"sample attachment\n", mime_type="text/plain")
message.save("example-message.msg")

Đọc và Chuyển Đổi MSG sang EML

Tải một tệp .msg hiện có bằng MapiMessage.from_file() và chuyển đổi nó thành một đối tượng EmailMessage chuẩn của Python thông qua to_email_message(). Từ đó, tuần tự hoá thành các byte EML để lưu trữ hoặc chuyển tiếp qua SMTP.

from aspose.email_foss import msg

with msg.MapiMessage.from_file("example-message.msg") as loaded:
    email_message = loaded.to_email_message()
    eml_bytes = email_message.as_bytes()

with open("example-message.eml", "wb") as f:
    f.write(eml_bytes)

Kiểm tra nội bộ MSG

Sử dụng MsgReaderCFBReader nền tảng của nó để kiểm tra cấu trúc nhị phân của tệp MSG. Truy cập siêu dữ liệu CFB (phiên bản, kích thước sector, số mục thư mục) và lặp lại các mục thuộc tính MAPI ở mức độ nhị phân.

from aspose.email_foss import msg

reader = msg.MsgReader.from_file("example-message.msg")
cfb = reader.cfb_reader

print(f"CFB major_version={cfb.major_version}")
print(f"sector_size={cfb.sector_size}")
print(f"directory_entries={cfb.directory_entry_count}")

for entry in reader.iter_top_level_fixed_length_properties():
    tag = entry.property_tag
    print(f"tag=0x{tag:08X} flags=0x{entry.flags:08X} value={entry.value.hex()}")

reader.close()

Truy cập CFB cấp thấp

Đọc và duyệt bất kỳ container Compound File Binary nào bằng cách sử dụng CFBReader. Đếm các storage và stream, giải quyết các đường dẫn theo tên, và trích xuất dữ liệu stream thô cho việc xử lý tùy chỉnh.

from aspose.email_foss.cfb import CFBReader

reader = CFBReader.from_file("example-message.msg")

for entry in reader.iter_storages():
    print(f"Storage: {entry.name}")

for entry in reader.iter_streams():
    data = reader.get_stream_data(entry.stream_id)
    print(f"Stream: {entry.name} size={len(data)}")

reader.close()

Bắt đầu nhanh

Cài đặt thư viện và tạo tệp MSG đầu tiên của bạn trong chưa đầy mười dòng:

pip install aspose-email-foss>=26.3
from aspose.email_foss import msg

message = msg.MapiMessage.create("Hello from Python", "This is a test message.")
message.set_property(msg.PropertyId.SENDER_EMAIL_ADDRESS, "sender@example.com")
message.add_recipient("recipient@example.com", display_name="Recipient")
message.save("hello.msg")

with msg.MapiMessage.from_file("hello.msg") as loaded:
    eml = loaded.to_email_message()
    print(eml["Subject"])

Định dạng được hỗ trợ

Định dạngNhậpXuất
MSG
CFB

Mã nguồn mở & Giấy phép

Aspose.Email FOSS for Python được phát hành dưới MIT License. Bạn có thể sử dụng nó trong các dự án cá nhân, nội bộ và thương mại mà không có hạn chế. Mã nguồn có sẵn trên GitHub.


Bắt đầu