การจัดการขั้นสูงของข้อความอีเมล

แสดงข้อมูลในลำดับที่กำหนดเองในไฟล์ MHTML

นี้ rendering_headers คุณสมบัติของ MhtSaveOptions class returns the list of headers used for rendering. Add the headers to it with the values of the MhtTemplateName class. The order in which the headers are added defines the order in which the information is displayed in the output file.

โค้ดตัวอย่างต่อไปนี้บันทึกข้อความเดียวกันสามครั้ง: ด้วยลำดับหัวข้อเริ่มต้น, ด้วยลำดับหัวข้อที่กำหนดเอง, และด้วยชุดหัวข้อที่แตกต่างอย่างสิ้นเชิง

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)

บันทึกส่วนหัวทั้งหมดใน MHTML

นี้ save_all_headers คุณสมบัติของ MhtSaveOptions class defines whether all the headers of the message have to be written to the output MHTML file. The following code snippet shows you how to save all headers:

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)

รับ Preamble และ Epilogue จากข้อความ EML

อีเมลอาจมีข้อมูลที่ซ่อนอยู่เป็นข้อความธรรมดาก่อนเนื้อความ (ส่วนต้น) หรือหลังเนื้อความ (ส่วนท้าย) ซึ่งมักเป็นข้อมูลเสริมหรือบริบทที่ให้ผู้รับก่อนหรือหลังอ่านเนื้อหาหลักของอีเมล คุณสามารถดึงข้อมูลนี้ผ่าน preamble และ epilogue properties of the MailMessage คลาส.

คุณสมบัติทั้งสองเป็นสตริงและอ่าน/เขียนได้ ดังนั้นจึงสามารถใช้ตั้งค่าส่วนต้นของข้อความและส่วนท้ายของข้อความที่คุณเขียนได้ โค้ดตัวอย่างต่อไปนี้แสดงวิธีดึงข้อความส่วนต้นและส่วนท้าย:

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))

การติดตามอีเมลโดยใช้ MDN และใบรับรองการอ่าน

Aspose.Email รองรับการติดตามอีเมลผ่าน Message Disposition Notification (MDN) ซึ่งทำได้โดยการร้องขอการรับทราบการอ่านและสร้างข้อมูลที่จำเป็น

  • นี้ read_receipt_to คุณสมบัติของ MailMessage class gets or sets the read receipt addresses. It is a MailAddressCollection, ดังนั้นจึงสามารถเพิ่มหลายที่อยู่เข้าไปได้
  • นี้ create_read_receipt method builds a read receipt for a received message.
  • นี้ read_receipt_requested คุณสมบัติของ MapiMessage class gets or sets whether a read receipt is requested for an Outlook message.

โค้ดตัวอย่างต่อไปนี้แสดงวิธีติดตามอีเมลด้วย 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)