Quản Lý Nâng Cao Tin Nhắn Email
Hiển Thị Thông Tin theo Thứ Tự Tùy Chỉnh trong Tệp MHTML
Cái rendering_headers thuộc tính của MhtSaveOptions lớp trả về danh sách các tiêu đề dùng để render. Thêm các tiêu đề vào đó với các giá trị của MhtTemplateName lớp. Thứ tự các tiêu đề được thêm vào xác định thứ tự hiển thị thông tin trong tệp đầu ra.
Đoạn mã sau lưu cùng một tin nhắn ba lần: với thứ tự tiêu đề mặc định, với thứ tự tiêu đề tùy chỉnh, và với một tập tiêu đề hoàn toàn khác.
import aspose.email as ae
eml = ae.MailMessage.load(data_dir + "Attachments.eml")
options = ae.MhtSaveOptions()
eml.save(data_dir + "CustomOrderOfInformationInMHTML_1.mhtml", options)
options.rendering_headers.append(ae.MhtTemplateName.FROM)
options.rendering_headers.append(ae.MhtTemplateName.SUBJECT)
options.rendering_headers.append(ae.MhtTemplateName.TO)
options.rendering_headers.append(ae.MhtTemplateName.SENT)
eml.save(data_dir + "CustomOrderOfInformationInMHTML_2.mhtml", options)
options.rendering_headers.clear()
options.rendering_headers.append(ae.MhtTemplateName.ATTACHMENTS)
options.rendering_headers.append(ae.MhtTemplateName.CC)
options.rendering_headers.append(ae.MhtTemplateName.SUBJECT)
eml.save(data_dir + "CustomOrderOfInformationInMHTML_3.mhtml", options)
Lưu Tất Cả Tiêu Đề trong MHTML
Cái save_all_headers thuộc tính của MhtSaveOptions lớp xác định liệu tất cả các tiêu đề của tin nhắn có phải được ghi vào tệp MHTML đầu ra hay không. Đoạn mã sau cho bạn thấy cách lưu tất cả tiêu đề:
import aspose.email as ae
eml = ae.MailMessage.load(data_dir + "message.eml")
save_options = ae.MhtSaveOptions()
save_options.save_all_headers = True
eml.save(data_dir + "message.mhtml", save_options)
Lấy Preamble và Epilogue từ Tin Nhắn EML
Một tin nhắn email có thể chứa một số thông tin ẩn dưới dạng văn bản thuần trước nội dung chính (phần mở đầu) hoặc sau nó (phần kết). Thông thường đó là thông tin bổ sung hoặc ngữ cảnh được cung cấp cho người nhận trước hoặc sau khi họ đọc nội dung chính của email. Bạn có thể lấy thông tin này qua preamble và epilogue thuộc tính của MailMessage lớp.
Cả hai thuộc tính đều là chuỗi và có thể đọc/ghi, do đó chúng cũng có thể được sử dụng để đặt phần mở đầu và phần kết của tin nhắn bạn soạn. Đoạn mã sau cho thấy cách lấy văn bản phần mở đầu và phần kết:
import aspose.email as ae
eml = ae.MailMessage.load(data_dir + "message.eml")
# Get the preamble text
print("Preamble: " + str(eml.preamble))
# Get the epilogue text
print("Epilogue: " + str(eml.epilogue))
Theo Dõi Email bằng MDN và Biên Nhận Đọc
Aspose.Email hỗ trợ theo dõi email thông qua Thông báo Giao nhận Tin nhắn (MDN). Điều này được thực hiện bằng cách yêu cầu các biên nhận đọc và tạo thông tin cần thiết.
- Cái
read_receipt_tothuộc tính của MailMessage lớp lấy hoặc đặt các địa chỉ biên nhận đọc. Nó là một MailAddressCollection, vì vậy có thể thêm nhiều địa chỉ vào đó. - Cái
create_read_receiptphương thức tạo biên nhận đọc cho một tin nhắn đã nhận. - Cái
read_receipt_requestedthuộc tính của MapiMessage lớp lấy hoặc đặt việc yêu cầu biên nhận đọc cho một tin nhắn Outlook.
Đoạn mã sau cho bạn thấy cách theo dõi một email bằng Aspose.Email:
import aspose.email as ae
from aspose.email.clients.smtp import SmtpClient
from aspose.email.mapi import MapiMessage, MapiConversionOptions
client = SmtpClient("smtp.server.com", 587, "username", "password")
# Send a message with the requested read receipt
mail_message = ae.MailMessage("from@domain.com", "to@domain.com", "test MDN",
"This is a test message with read receipt requested")
read_receipt_addresses = ae.MailAddressCollection()
read_receipt_addresses.append(ae.MailAddress("from@domain.com"))
mail_message.read_receipt_to = read_receipt_addresses
client.send(mail_message)
# Several ReadReceiptTo addresses can be added to the collection
read_receipt_addresses.append(ae.MailAddress("another@domain.com"))
mail_message.read_receipt_to = read_receipt_addresses
client.send(mail_message)
# On the recipient side: check the request, then create and send the read receipt
eml_mdn = ae.MailMessage.load(data_dir + "received.eml")
if len(eml_mdn.read_receipt_to) > 0:
client.send(eml_mdn.create_read_receipt("to@domain.com", None))
# Create a MapiMessage with a requested read receipt
mapi_message = MapiMessage("from@domain.com", "to@domain.com", "test MDN",
"This is a read requested mapiMessage")
mapi_message.read_receipt_requested = True
# Create a MailMessage with a requested read receipt and convert it to MapiMessage
mapi_message = MapiMessage.from_mail_message(mail_message, MapiConversionOptions.unicode_format)
Các tính năng sau của Aspose.Email cho .NET dựa trên delegate .NET và không được lộ ra trong Aspose.Email cho Python qua .NET:
- theo dõi tiến độ chuyển đổi tài liệu qua
SaveOptions.CustomProgressHandler, - render siêu liên kết với kiểu tùy chỉnh qua
HyperlinkRenderingCallback, - xử lý việc lưu tài nguyên qua
HtmlSaveOptions.SaveResourceHandlervàResourceHtmlRenderingsự kiện.