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

The samples in this article use these headers and namespaces:

#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <EmlLoadOptions.h>
#include <HeaderType.h>
#include <Mime/HeaderCollection.h>
#include <system/convert.h>
#include <system/text/encoding.h>

using namespace System;
using namespace Aspose::Email;

Display Email Information

Load an email file and display its key properties on the screen, such as the sender, recipients, subject, and body.

  1. Load the email message into a MailMessage with MailMessage::Load.
  2. Read the desired properties - for example From, To, Subject, HtmlBody and Body.

The recipients are a MailAddressCollection; its ToString gives the whole list in the form used in a message header.

SharedPtr<MailMessage> message = MailMessage::Load(u"Message.eml", MakeObject<EmlLoadOptions>());

Console::WriteLine(u"From: " + message->get_From()->ToString());
Console::WriteLine(u"To: " + message->get_To()->ToString());
Console::WriteLine(u"Subject: " + message->get_Subject());
Console::WriteLine(u"HtmlBody: " + message->get_HtmlBody());
Console::WriteLine(u"TextBody: " + message->get_Body());

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.

To read the headers:

  1. Load an email file into a MailMessage.
  2. Retrieve the header collection using the get_Headers() method.
  3. Iterate through the collection: enumerating it yields the header names, and Get returns the value at a position or the value of a named header.
SharedPtr<MailMessage> message = MailMessage::Load(u"email-headers.eml", MakeObject<EmlLoadOptions>());
SharedPtr<Mime::HeaderCollection> headers = message->get_Headers();

int32_t index = 0;
for (String name : headers)
{
    Console::WriteLine(name + u" - " + headers->Get(index++));
}

// a single header by its name; HeaderType names the standard ones
Console::WriteLine(u"Subject header: " + headers->Get(HeaderType::get_Subject()->ToString()));

Get Decoded Header Values

Header values that contain non-ASCII text are stored as encoded words (RFC 2047), for example =?utf-8?B?...?=. Get returns the value as it is stored, while GetDecodedValue() of the HeaderCollection class returns the text a reader expects. For a value without encoded words, both return the same.

SharedPtr<MailMessage> mailMessage = MailMessage::Load(u"emlWithHeaders.eml");
Console::WriteLine(u"Thread-Topic: " + mailMessage->get_Headers()->GetDecodedValue(u"Thread-Topic"));

// a header with an encoded word
String encoded = u"=?utf-8?B?" + Convert::ToBase64String(Text::Encoding::get_UTF8()->GetBytes(u"Grüße aus Köln")) + u"?=";
mailMessage->get_Headers()->Add(u"X-Greeting", encoded);

Console::WriteLine(u"Stored value:  " + mailMessage->get_Headers()->Get(u"X-Greeting"));
Console::WriteLine(u"Decoded value: " + mailMessage->get_Headers()->GetDecodedValue(u"X-Greeting"));

Extract the Message Body

Two properties expose the text of a message, and they behave differently:

  • Body returns the text/plain part when the message has one. If it has none, the plain text is derived from the HTML body by dropping the markup.
  • GetHtmlBodyText always works on HtmlBody and converts it to plain text. Its bool argument decides whether the targets of the links are kept in the result.
SharedPtr<MailMessage> mail = MailMessage::Load(u"HtmlWithUrlSample.eml");

// text/plain part if the message has one, otherwise the HTML body stripped of markup
String plainTextBody = mail->get_Body();
Console::WriteLine(plainTextBody);

// the HTML body as plain text, with and without the link targets
String bodyWithUrl = mail->GetHtmlBodyText(true);
String bodyWithoutUrl = mail->GetHtmlBodyText(false);

Console::WriteLine(u"Body with URL: " + bodyWithUrl);
Console::WriteLine(u"Body without URL: " + bodyWithoutUrl);

The same method also accepts a callback that decides how each hyperlink is rendered; see Email Messages Advanced Management.

Read the X.500 Exchange Address

Messages that travelled through Exchange often carry the sender and the recipients in the X.500 directory form (/o=.../ou=.../cn=...) instead of, or in addition to, an SMTP address. MailAddress exposes it through X500Address; the property is an empty string when the message carries no such address.

SharedPtr<MailMessage> mailMessage = MailMessage::Load(u"Contact.msg");

// the Exchange address of the sender, if the message carries one
String exchangeAddress = mailMessage->get_From()->get_X500Address();

if (!String::IsNullOrEmpty(exchangeAddress))
{
    Console::WriteLine(u"X.500 address: " + exchangeAddress);
}
else
{
    Console::WriteLine(u"The message has no X.500 address; SMTP address: " + mailMessage->get_From()->get_Address());
}