Làm việc với các tệp PST lớn
Contents
[
Hide
]
Hiệu năng có thể bị giảm khi xử lý các tệp PST lớn. Các giải pháp dưới đây sẽ giúp bạn cải thiện hiệu năng của ứng dụng khi xử lý các tệp lưu trữ lớn.
Ưu tiên các phương pháp liệt kê khi duyệt các thư mục hoặc tin nhắn của PST.
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for message_info in folder.enumerate_messages():
# Do something with the message
print(message_info.subject)
Ưu tiên MessageInfo để truy cập các thuộc tính tin nhắn cơ bản.
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for message_info in folder.enumerate_messages():
print(f"Subject: {message_info.subject}")
print(f"To: {message_info.display_to}")
print(f"Importance: {message_info.importance}")
print(f"Message Class: {message_info.message_class}")
Tránh gọi
extract_message hoặc enumerate_mapi_messages đối với tất cả tin nhắn trừ khi bạn cần truy cập tất cả các thuộc tính của chúng.
Xem xét việc sử dụng
enumerate_messages_entry_id để lấy tất cả ID tin nhắn có trong một thư mục.
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for entry_id in folder.enumerate_messages_entry_id():
# Use the id to retrieve a single property (extract_property),
# extract a MapiMessage (extract_message),
# extract the message attachments (extract_attachments),
# or save the message to a stream (save_message_to_stream).
pass
Xem xét việc sử dụng
extract_property để đọc một thuộc tính duy nhất còn thiếu trong MessageInfo.
import base64
from aspose.email.mapi import KnownPropertyList
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for msg_id in folder.enumerate_messages_entry_id():
transport_message_headers = pst.extract_property(
base64.b64decode(msg_id),
KnownPropertyList.TRANSPORT_MESSAGE_HEADERS.tag)
Xem xét việc sử dụng
extract_attachments nếu chỉ cần các tệp đính kèm.
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for msg_id in folder.enumerate_messages_entry_id():
attachments = pst.extract_attachments(msg_id)
Sử dụng lọc dựa trên tìm kiếm để chỉ lấy những tin nhắn bạn cần.
from aspose.email.mapi import MapiMessageFlags
from aspose.email.storage.pst import PersonalStorage, PersonalStorageQueryBuilder
with PersonalStorage.from_file("storage.pst") as pst:
builder = PersonalStorageQueryBuilder()
# Unread messages
builder.has_no_flags(MapiMessageFlags.READ)
for folder in pst.root_folder.enumerate_folders():
unread = folder.get_contents(builder.get_query())
Xem xét việc sử dụng
save_message_to_stream khi cần lưu các tin nhắn từ PST.
Thay vì sử dụng:
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for entry_id in folder.enumerate_messages_entry_id():
msg = pst.extract_message(entry_id)
msg.save("message.msg")
sử dụng:
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for entry_id in folder.enumerate_messages_entry_id():
with open("message.msg", "wb") as stream:
pst.save_message_to_stream(entry_id, stream)
Aspose.Email cho .NET cũng cung cấp các thao tác PST bất đồng bộ —
CreateAsync, FromFileAsync, MergeWithAsync và SplitIntoAsync. Các phương pháp này dựa trên các tác vụ .NET và token hủy và không được lộ ra trong Aspose.Email cho Python qua .NET; hãy sử dụng các phiên bản đồng bộ thay thế.