Quickstart
Quickstart: Your First MSG File in C++
This guide creates a complete, runnable example — composing an email, saving it as an MSG file, loading it back, and converting it to EML — using only the standard C++ library and Aspose.Email FOSS for C++.
Prerequisites
| Requirement | Detail |
|---|---|
| Compiler | C++17 or later (GCC 9+, Clang 10+, MSVC 2019+) |
| Build system | CMake 3.26 or later |
| Library | Cloned from GitHub (see Installation) |
| Dependencies | None |
Step 1: Set Up Your CMakeLists.txt
cmake_minimum_required(VERSION 3.26)
project(email_quickstart)
add_subdirectory(Aspose.Email-FOSS-for-Cpp)
add_executable(quickstart main.cpp)
target_link_libraries(quickstart PRIVATE AsposeEmailFoss::AsposeEmailFoss)
set_target_properties(quickstart PROPERTIES CXX_STANDARD 17)Step 2: Create and Save an MSG File
Create a new MSG file with subject, body, sender, a recipient, and an attachment:
#include <fstream>
#include <vector>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
// Compose the message
auto message = aspose::email::foss::msg::mapi_message::create(
"Hello from Aspose.Email FOSS",
"This message was created programmatically — no Outlook required.");
message.set_sender_name("Alice");
message.set_sender_email_address("alice@example.com");
message.add_recipient("bob@example.com", "Bob");
message.add_attachment(
"readme.txt",
std::vector<std::uint8_t>{'H', 'e', 'l', 'l', 'o', '\n'},
"text/plain");
// Save to MSG file
std::ofstream out("hello.msg", std::ios::binary);
message.save(out);
return 0;
}Build and run:
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/quickstartThe file hello.msg is created in the working directory. Open it in Outlook to verify,
or continue to step 3 to read it back programmatically.
Step 3: Read the MSG File Back
Load the file you just created and print its properties:
#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
std::ifstream input("hello.msg", std::ios::binary);
const auto message = aspose::email::foss::msg::mapi_message::from_stream(input);
std::cout << "Subject: " << message.subject() << '\n';
std::cout << "From: " << message.sender_name()
<< " <" << message.sender_email_address() << ">\n";
std::cout << "Body: " << message.body() << '\n';
std::cout << "Recipients: " << message.recipients().size() << '\n';
std::cout << "Attachments: " << message.attachments().size() << '\n';
for (const auto& a : message.attachments())
std::cout << " - " << a.filename << " (" << a.data.size() << " bytes)\n";
return 0;
}Expected output:
Subject: Hello from Aspose.Email FOSS
From: Alice <alice@example.com>
Body: This message was created programmatically — no Outlook required.
Recipients: 1
Attachments: 1
- readme.txt (6 bytes)Step 4: Convert to EML
Convert the MSG file to RFC 5322 / MIME format with save_to_eml():
#include <filesystem>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
const auto message = aspose::email::foss::msg::mapi_message::from_file(
std::filesystem::path("hello.msg"));
message.save_to_eml(std::filesystem::path("hello.eml"));
std::cout << "Saved hello.eml\n";
return 0;
}Subject, body, sender, recipients, and the attachment are all preserved in the EML output.
Next Steps
- Installation — full CMake project setup and supported platforms
- Features — complete feature reference with C++ examples
- Developer Guide — MSG, EML, CFB, and MAPI property walkthroughs
- FAQ — licensing, format support, known limitations