Read and Display Email Messages & Headers in C++

Aspose.Email for C++ provides the MailMessage class, which represents an email message and allows developers to access and display its contents and headers. You can easily extract information such as sender, recipients, subject, body, and headers from an existing email file (EML).

Display Email Information

Load an email file and display its key properties on the screen, such as the sender, recipients, subject, and body. The code sample below will show you how to display email information on the screen using Aspose.Email API.

  1. Create an instance of the MailMessage class.
  2. Load an email message into the MailMessage instance.
  3. Display the desired properties (for example, From, To, Subject, and Body) on the console.

Note: For complete examples and data files, visit the Aspose.Email for C++ GitHub repository.

Extract Email Headers

An email header is a set of metadata fields that describe the message, including routing information, content type, encoding, and sender/recipient details.

Aspose.Email for C++ allows you to extract and work with these headers using the MailMessage class.

The most common header types are available through the HeaderType class, which provides named constants for standard header fields.

The following code snippet shows how to extract email headers:

  1. Create an instance of the MailMessage class.
  2. Load an email file into the instance.
  3. Retrieve the header collection using the get_Headers() method.
  4. Iterate through the collection to read or display header names and values.

Get Decoded Header Values

Some email headers may contain encoded text (for example, subject lines or custom headers using encoded words). You can easily retrieve a decoded value using the GetDecodedValue() method of the HeaderCollection class.

The following code snippet shows you how to get decoded header values.