Quản Lý Các Tệp Tin Nhắn với Aspose.Email.Outlook
Xác định loại mục MAPI trong thư mục PST
Một tệp PST thường chứa các loại dữ liệu khác nhau, như tin nhắn email, sự kiện lịch, danh bạ và hơn nữa. MapiItemType lớp của Aspose.Email cho phép bạn truy cập và phân loại các loại mục MAPI khác nhau trong tệp PST. Đoạn mã dưới đây minh họa cách xác định loại mục MAPI trong một thư mục PST:
import aspose.email as ae
pst = ae.storage.pst.PersonalStorage.from_file("test.pst")
folder = pst.root_folder.get_sub_folder("Calendar")
for messageInfo in folder.enumerate_messages():
msg = pst.extract_message(messageInfo)
# Get the class type based on msg.SupportedType
item_type = msg.supported_type
# Non-supported type. It cannot be accessed as appropriate item type.
if item_type == ae.mapi.MapiItemType.NONE:
print("Item type not supported")
# An email message.
elif item_type == ae.mapi.MapiItemType.MESSAGE:
# You can access to MapiMessage properties there.
# A subject for example
print(msg.subject)
# A contact item. Can be accessed as MapiContact.
elif item_type == ae.mapi.MapiItemType.CONTACT:
contact = msg.to_mapi_message_item()
# You can access to MapiContact properties there.
# A name_info.display_name for example.
print(contact.name_info.display_name)
# A calendar item. Can be accessed as MapiCalendar.
elif item_type == ae.mapi.MapiItemType.CALENDAR:
calendar = msg.to_mapi_message_item()
# You can access to MapiCalendar properties there.
# A location for example.
print(calendar.location)
# A distribution list. Can be accessed as MapiDistributionList.
elif item_type == ae.mapi.MapiItemType.DIST_LIST:
dlist = msg.to_mapi_message_item()
# You can access to MapiDistributionList properties there
# A Journal entry. Can be accessed as MapiJournal.
elif item_type == ae.mapi.MapiItemType.JOURNAL:
journal = msg.to_mapi_message_item()
# You can access to MapiJournal properties there
# A StickyNote. Can be accessed as MapiNote.
elif item_type == ae.mapi.MapiItemType.NOTE:
note = msg.to_mapi_message_item()
# You can access to MapiNote properties there
# A Task item. Can be accessed as MapiTask.
elif item_type == ae.mapi.MapiItemType.TASK:
task = msg.to_mapi_message_item()
# You can access to MapiTask properties there
Chuyển Đổi MSG sang Tin MIME
API Aspose.Email cung cấp khả năng chuyển đổi tệp MSG sang tin nhắn MIME bằng phương thức ToMailMessage.
Đặt thời gian chờ cho việc chuyển đổi và tải tin nhắn
Để giới hạn thời gian chuyển đổi tin nhắn tính bằng mili giây (giá trị mặc định 3 giây), thuộc tính timeout của MailConversionOptions lớp được sử dụng.
Dưới đây là cách bạn có thể đặt thời gian chờ cho các quá trình chuyển đổi và tải tin nhắn:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("my.msg")
options = ae.mapi.MailConversionOptions()
options.timeout = 5000
mailMessage = msg.to_mail_message(options)
Bằng cách đặt thời gian chờ cho các quá trình chuyển đổi và tải tin nhắn, bạn có thể kiểm soát thời gian tối đa mà các thao tác này được phép chạy. Sau khi đặt thời gian chờ, bạn có thể tiếp tục các quá trình chuyển đổi và tải tin nhắn của mình.
Xử lý tệp Outlook Template (OFT)
Tải, Sửa đổi và Lưu một Tệp OFT
Các mẫu Outlook (OFT) cung cấp một cách tiện lợi để tối ưu hóa quy trình gửi các email lặp lại. Thay vì soạn cùng một email từ đầu mỗi lần, bạn có thể tạo một tin nhắn trong Outlook và lưu nó dưới dạng Outlook Template (OFT). Sau này, khi cần gửi một tin nhắn tương tự, bạn có thể nhanh chóng tạo ra từ mẫu. Cách này giúp bạn tiết kiệm công sức việc viết lại cùng nội dung trong phần thân tin nhắn, xác định tiêu đề, định dạng và các yếu tố khác.
Aspose.Email MailMessage lớp cung cấp công cụ mạnh mẽ để tải và thao tác các tệp mẫu Outlook (OFT). Khi một mẫu Outlook được tải vào một thể hiện của lớp MailMessage, bạn có thể dễ dàng cập nhật các thuộc tính như người gửi, người nhận, nội dung tin nhắn, chủ đề và các thuộc tính khác.
import aspose.email as ae
# Load the OFT file
oft_file_path = "your_template.oft"
mail_message = ae.MailMessage.load(oft_file_path)
# Update properties as needed
mail_message.subject = "Updated Subject"
mail_message.body = "Updated body text."
# Save the updated message as an MSG file
msg_file_path = "updated_message.msg"
mail_message.save(msg_file_path, ae.MailMessageSaveType.outlook_message_format_unicode)
print(f"Updated message saved to {msg_file_path}")
Sau khi thực hiện các cập nhật cần thiết, bạn có thể gửi email bằng SmtpClient lớp:
# Send the email
smtpClient.send(message)
Lưu Tệp MSG Outlook dưới dạng Mẫu
Đối với mục đích này, bạn có thể sử dụng MailMessage lớp để tạo một email, sau đó lưu nó dưới dạng tệp OFT. Mẫu mã dưới đây sẽ cho bạn thấy cách lưu email dưới dạng Outlook Template:
import aspose.email as ae
# Create a new MailMessage
message = ae.MailMessage()
message.subject = "Sample Outlook Template"
message.html_body = "<html><body>This is the body of the email.</body></html>"
message.from_address = ae.MailAddress("your.email@example.com", "Your Name")
# Save the MailMessage as an Outlook Template (OFT) file
oft_file_path = "sample_template.oft"
message.save(oft_file_path, ae.SaveOptions.default_oft)
print(f"Outlook Template saved as {oft_file_path}")
OFT hay MSG: Xác định loại của một MapiMessage
Đoạn mã dưới đây minh họa cách xác định xem một tin nhắn MAPI đã tải có phải là tin nhắn email tiêu chuẩn hay là Outlook Template (OFT) không:
msg = ae.mapi.MapiMessage.Load("message.msg");
isOft = msg.is_template # returns false
msg = ae.mapi.MapiMessage.Load("message.oft");
isOft = msg.IsTemplate; # returns true
Sau khi tải tệp MSG, mã kiểm tra xem thuộc tính is_template của MapiMessaage lớp trả về True hoặc False. Nếu trả về false, tin nhắn được tải là một tin thư chuẩn, không phải Mẫu Outlook; ngược lại, nếu trả về true, tin nhắn không phải là tin thư chuẩn mà là một Mẫu Outlook.
Lưu MapiMessage hoặc MailMessage dưới dạng OFT
Cái SaveOptions là một lớp cơ sở trừu tượng cho các lớp cho phép người dùng chỉ định các tùy chọn bổ sung khi lưu MailMessage vào một định dạng cụ thể. Mẫu mã dưới đây minh họa cách lưu tin nhắn dưới định dạng OFT:
import aspose.email as ae
# Save the MailMessage to OFT format
eml = ae.MailMessage.load("message.eml")
eml.save("message.oft", ae.SaveOptions.default_oft)
# or alternative way 2
save_options = ae.MsgSaveOptions(ae.MailMessageSaveType.outlook_template_format)
eml.save("message.oft", save_options)
# or alternative way 3
save_options = ae.SaveOptions.create_save_options(ae.MailMessageSaveType.outlook_template_format)
eml.save("message.oft", save_options)
# Save the MapiMessage to OFT format
msg = ae.mapi.MapiMessage.load("message.msg")
msg.save("message.oft", ae.SaveOptions.default_oft)
# or alternative way 2
save_options = ae.MsgSaveOptions(ae.MailMessageSaveType.outlook_template_format)
msg.save("message.oft", save_options)
# or alternative way 3
save_options = ae.SaveOptions.create_save_options(ae.MailMessageSaveType.outlook_template_format)
msg.save("message.oft", save_options)
Quản lý tin nhắn với chữ ký số
Thư viện cung cấp LoadOptions lớp, một lớp cơ sở trừu tượng cho các lớp cho phép người dùng chỉ định các tùy chọn bổ sung khi tải MailMessage từ một định dạng cụ thể. Tùy chọn giữ chữ ký được đặt mặc định.
Chuyển đổi từ EML sang MSG giữ chữ ký
Đoạn mã mẫu bên dưới minh họa cách tải một tin nhắn, chuyển đổi nó sang định dạng MSG và lưu dưới dạng MSG (chữ ký được giữ nguyên theo mặc định):
import aspose.email as ae
# Load mail message
loadOptions = ae.EmlLoadOptions()
message = ae.MailMessage.load("Message.eml", loadOptions)
# Save as MSG
message.save("ConvertEMLToMSG_out.msg", ae.SaveOptions.default_msg_unicode)
Chuyển đổi tin nhắn S/MIME từ MSG sang EML
Aspose.Email cho phép bạn chuyển đổi từ MSG sang EML giữ nguyên chữ ký số như được minh họa trong đoạn mã sau.
import aspose.email as ae
# Load mail message
loadOptions = ae.EmlLoadOptions()
smime_eml = ae.MailMessage.load("smime.eml", loadOptions)
conversion_options = ae.mapi.MapiConversionOptions(ae.mapi.OutlookMessageFormat.UNICODE)
msg = ae.mapi.MapiMessage.from_mail_message(smime_eml, conversion_options)
# Save File to disk
msg.save("ConvertMIMEMessagesFromMSGToEML_out.msg")
Đặt Danh mục Màu cho Tệp Outlook MSG
Đôi khi bạn có thể cần phân biệt các email quan trọng và sắp xếp chúng một cách trực quan. Thư viện cung cấp cách thực hiện điều này bằng cách gán một màu cụ thể cho một mục tin nhắn. Khi bạn đặt danh mục màu cho một mục, nó cho phép bạn dễ dàng nhận diện và tìm kiếm các mục liên quan ngay lập tức. Sử dụng FollowUpManager lớp để đặt danh mục màu cho một tin nhắn như trong ví dụ mã sau:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("my.msg")
# Add Two categories
ae.mapi.FollowUpManager.add_category(msg, "Purple Category")
ae.mapi.FollowUpManager.add_category(msg, "Red Category")
# Retrieve the list of available categories
categories = ae.mapi.FollowUpManager.get_categories(msg)
# Remove the specified category and then Clear all categories
ae.mapi.FollowUpManager.remove_category(msg, "Red Category")
ae.mapi.FollowUpManager.clear_categories(msg)
Truy cập thông tin Follow Up từ tệp MSG
Trích xuất Thông tin Đọc và Biên nhận Giao hàng
Mẫu mã dưới đây minh họa cách trích xuất thông tin người nhận và trạng thái theo dõi của họ từ tệp tin Outlook (MSG):
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("received.msg")
for recipient in msg.recipients:
print(f"Recipient: {recipient.display_name}")
print(f"Delivery time: {recipient.properties[ae.mapi.MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME_DELIVERY]}")
print(f"Read time: {recipient.properties[ae.mapi.MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME_READ]}")
Tạo Tin Nhắn Forward và Reply
Aspose.Email cung cấp các cách đơn giản để tạo tin nhắn chuyển tiếp và trả lời dựa trên các tin nhắn hiện có.
Tạo Tin Nhắn Chuyển Tiếp
Bạn có thể sử dụng ForwardMessageBuilder lớp để tạo tin chuyển tiếp bằng cách đặt tin gốc, người gửi, người nhận, tiêu đề và nội dung. Tin chuyển tiếp có thể bao gồm tin gốc dưới dạng tệp đính kèm hoặc là nội dung của tin chuyển tiếp. Bạn có thể linh hoạt tùy chỉnh các thuộc tính bổ sung như tệp đính kèm, tiêu đề và tùy chọn định dạng. Đoạn mã dưới đây cho thấy cách tạo một tin chuyển tiếp:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("original.msg")
builder = ae.tools.ForwardMessageBuilder()
builder.addition_mode = ae.tools.OriginalMessageAdditionMode.TEXTPART
forwardMsg = builder.build_response(msg)
forwardMsg.save("forward_out.msg")
Tạo tin nhắn trả lời
Cái ReplyMessageBuilder lớp được dùng để cấu hình cài đặt trả lời, bao gồm tin gốc, người gửi, người nhận, chế độ trả lời, tiền tố tiêu đề và nội dung tin trả lời. Tin trả lời có thể được tạo ở các chế độ khác nhau như "Trả lời người gửi" hoặc "Trả lời tất cả" tùy nhu cầu. Bạn có thể tùy chỉnh các thuộc tính như tệp đính kèm, tiêu đề và tùy chọn định dạng cho tin trả lời. Đoạn mã dưới đây cho thấy cách tạo một tin chuyển tiếp:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("original.msg")
builder = ae.tools.ReplyMessageBuilder()
# Set ReplyMessageBuilder Properties
builder.reply_all = True
builder.addition_mode = ae.tools.OriginalMessageAdditionMode.TEXTPART
builder.response_text = "<p><b>Dear Friend,</b></p> I want to do is introduce my co-author and co-teacher. <p><a href=\"www.google.com\">This is a first link</a></p><p><a href=\"www.google.com\">This is a second link</a></p>";
replyMsg = builder.build_response(msg)
replyMsg.save("reply_out.msg")