Extracting Email Information
Displaying Email Metadata
The MailMessage represents an email message and allows developers to access its properties. The header information can be extracted and manipulated in different ways. This article explains how to display selected email header information and the email body on the screen. To display email information on the screen, follow these steps:
- Create an instance of the MailMessage class.
- Load an email message into the
MailMessageinstance. - Display the email content on the screen.
The following code snippet shows you how to display email information on the screen:
Extracting Email Headers
The email header represents an Internet and RFC defined standard set of header fields included in email messages. An email header can be specified using the MailMessage class. Common header types are defined in the HeaderType class. It is a sealed class working like typical enumeration. To extract headers from an email, follow these steps:
- Create an instance of the MailMessage class.
- Load an email message in the instance of MailMessage class.
- After an email message has been loaded, we will get its raw content.
The MailMessage class itself contains properties such as From, To, Cc, Subject and so on. These properties can be extracted from headers.
The following code snippet shows you how to extract email headers:
Decoding Header Values
The following code snippet shows you how to get decoded header values.
Extracting the Email Body
Getting the Plain Text Body
The body property returns a plain text representation of a message body:
import aspose.email as ae
mail = ae.MailMessage.load(data_dir + "message.eml")
plain_text_body = mail.body
print(plain_text_body)
Note: if the text/plain MIME part is present in the message, the property returns its text data. Otherwise, it returns the text content separated from the html_body property without the HTML markup.
Getting the HTML Body as Plain Text
The MailMessage class provides the get_html_body_text method that returns the HTML body as plain text. The method parses the html_body property and returns the separated plain text content ignoring the HTML markup. It accepts a boolean parameter that indicates whether the returned body should contain URLs: pass True to keep them.
import aspose.email as ae
mail = ae.MailMessage.load(data_dir + "message.eml")
body_with_url = mail.get_html_body_text(True) # the body will contain URLs
body_without_url = mail.get_html_body_text(False) # the body will not contain URLs
print("Body with URL: " + body_with_url)
print("Body without URL: " + body_without_url)
Accessing the X.500 Exchange Address
The MailAddress class exposes the x500_address property, which returns the Exchange X.500 formatted address if it is present in the message metadata, and an empty value otherwise. No parsing is required — the property returns the raw X.500 string.
import aspose.email as ae
mail_message = ae.MailMessage.load(data_dir + "message.msg")
exchange_address = mail_message.from_address.x500_address
print(exchange_address)