Email Messages Advanced Management

Display Information in Custom Order in MHTML Files

The rendering_headers property of the 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.

The following code snippet saves the same message three times: with the default header order, with a custom header order, and with a completely different set of headers.

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)

Save All Headers in MHTML

The save_all_headers property of the 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)

Obtain Preamble and Epilogue from EML Messages

An email message may contain some hidden information as plain text before the message body (the preamble) or after it (the epilogue). It is usually additional information or context provided to the recipient before or after they read the main content of the email. You can obtain this information through the preamble and epilogue properties of the MailMessage class.

Both properties are strings and are read/write, so they can also be used to set the preamble and the epilogue of the message you compose. The following code snippet shows how to obtain the preamble and epilogue texts:

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

Email Tracking using MDN and Read Receipts

Aspose.Email supports email tracking through Message Disposition Notification (MDN). This is achieved by requesting the read receipts and creating the required information.

  • The read_receipt_to property of the MailMessage class gets or sets the read receipt addresses. It is a MailAddressCollection, so several addresses can be added to it.
  • The create_read_receipt method builds a read receipt for a received message.
  • The read_receipt_requested property of the MapiMessage class gets or sets whether a read receipt is requested for an Outlook message.

The following code snippet shows you how to track an email with 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)