Load, View and Parse Outlook MSG file using C++ Email Library

Loading, Viewing and Parsing MSG File

This topic explains how to load a Microsoft Outlook Messagefile (*.msg) using C++ Email Parser Library.

The MapiMessage class is used to load MSG files, and provides several static loading functions for different scenarios. The following code snippet shows you how to load MSG files from file or from stream.

Loading MSG Files

The following code snippet shows you how to load MSG files.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Outlook();
// Create an instance of MapiMessage from file
System::SharedPtr<MapiMessage> msg = MapiMessage::FromFile(dataDir + L"message.msg");
// Get subject
System::Console::WriteLine(System::String(L"Subject:") + msg->get_Subject());
// Get from address
System::Console::WriteLine(System::String(L"From:") + msg->get_SenderEmailAddress());
// Get body
System::Console::WriteLine(System::String(L"Body") + msg->get_Body());
// Get recipients information
System::Console::WriteLine(System::String(L"Recipient: ") + msg->get_Recipients());
// Get attachments
{
auto att_enumerator = (msg->get_Attachments())->GetEnumerator();
decltype(att_enumerator->get_Current()) att;
while (att_enumerator->MoveNext() && (att = att_enumerator->get_Current(), true))
{
System::Console::Write(System::String(L"Attachment Name: ") + att->get_FileName());
System::Console::Write(System::String(L"Attachment Display Name: ") + att->get_DisplayName());
}
}

Loading from Stream

The following code snippet shows you how to load file from stream.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// Create an instance of MapiMessage from file
System::ArrayPtr<uint8_t> bytes = System::IO::File::ReadAllBytes(dataDir + L"message.msg");
{
System::SharedPtr<System::IO::MemoryStream> stream = System::MakeObject<System::IO::MemoryStream>(bytes);
stream->Seek(0, System::IO::SeekOrigin::Begin);
// Create an instance of MapiMessage from file
System::SharedPtr<MapiMessage> msg = MapiMessage::FromStream(stream);
// Get subject
System::Console::WriteLine(System::String(L"Subject:") + msg->get_Subject());
// Get from address
System::Console::WriteLine(System::String(L"From:") + msg->get_SenderEmailAddress());
// Get body
System::Console::WriteLine(System::String(L"Body") + msg->get_Body());
}