介绍

在 C++ 中处理 Outlook MSG 文件历来需要 Windows 专用的 COM 自动化
或是带有严格许可的重量级第三方库。 Aspose.Email FOSS for C++
改变了这一点:它是一个纯 C++17 库,没有外部依赖、没有 COM,且
没有平台特定代码。它在 Linux、macOS 和 Windows 上的构建完全相同。

该库在 MIT 许可证下发布,并通过 GitHub 以源代码形式分发。您 通过克隆仓库并将其添加为 CMake 子目录来集成它——无需注册包管理器,也不需要预先构建二进制文件。

本文介绍了核心工作流程:读取现有的 MSG 文件,检查其属性,编程创建新消息,以及在 MSG 和 EML 格式之间来回转换。


关键特性

读取现有 MSG 文件

使用 msg_reader 打开 .msg 文件,并使用 msg_document 访问其结构化内容。
两个类位于 aspose::email::foss::msg 命名空间中。

#include "aspose/email/foss/msg/msg_reader.hpp"
#include "aspose/email/foss/msg/msg_document.hpp"
#include <filesystem>

namespace aef = aspose::email::foss;

auto reader   = aef::msg::msg_reader::from_file(std::filesystem::path("message.msg"));
auto document = aef::msg::msg_document::from_reader(reader);

msg_reader 还有一个 from_stream 重载,用于从 std::istream 读取,这在服务器端或管道环境中非常有用,因为文件不在磁盘上。

以编程方式创建消息

mapi_message::create 生成一个带有主题和正文的内存中新消息。您
可以随后设置发件人详情,添加收件人,并在保存之前附加二进制或文本负载。

#include "aspose/email/foss/msg/mapi_message.hpp"

namespace aef = aspose::email::foss;

auto msg = aef::msg::mapi_message::create(
    "Quarterly status update",
    "Hello team,\n\nPlease find the rollout summary attached.\n\nRegards");

msg.set_sender_name("Build Agent");
msg.set_sender_email_address("build.agent@example.com");
msg.add_recipient("alice@example.com", "Alice Example");
msg.save(std::filesystem::path("output.msg"));

将 MSG 转换为 EML

mapi_message 支持通过 save_to_eml 方法直接将 MSG 转换为 EML。
EML 输出符合 RFC 5322(MIME),使其兼容任何标准邮件
客户端或处理流水线。

auto loaded = aef::msg::mapi_message::from_file(
    std::filesystem::path("message.msg"), /*strict=*/false);

// Save as EML to a file
loaded.save_to_eml(std::filesystem::path("message.eml"));

// Or capture as bytes
std::vector<std::uint8_t> eml_bytes = loaded.save_to_eml();

从 EML 加载

反向方向同样直接:load_from_eml 读取 EML 文件并在内存中生成 mapi_message,随后可以将其保存回 MSG。

auto from_eml = aef::msg::mapi_message::load_from_eml(
    std::filesystem::path("message.eml"));

from_eml.save(std::filesystem::path("restored.msg"));

低级 CFB 访问

对于高级用例——例如读取高级 API 未公开的原始 MAPI 属性——cfb_reader 提供对支撑 MSG 格式的复合文件二进制容器的直接访问。

#include "aspose/email/foss/cfb/cfb_reader.hpp"

namespace aef = aspose::email::foss;

auto cfb = aef::cfb::cfb_reader::from_file(std::filesystem::path("message.msg"));
auto storage_ids = cfb.storage_ids();
auto stream_ids  = cfb.stream_ids();

快速入门

步骤 1 — 克隆并添加为子目录

git clone https://github.com/aspose-email-foss/Aspose.Email-FOSS-for-Cpp.git

在您的项目 CMakeLists.txt

cmake_minimum_required(VERSION 3.26)
project(my_project)

add_subdirectory(Aspose.Email-FOSS-for-Cpp)

add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE AsposeEmailFoss::AsposeEmailFoss)

步骤 2 — 包含并使用

#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"

namespace aef = aspose::email::foss;

int main()
{
    // Create a message
    auto msg = aef::msg::mapi_message::create("Hello from C++", "Body text here");
    msg.save(std::filesystem::path("hello.msg"));

    // Round-trip to EML
    auto loaded = aef::msg::mapi_message::from_file(
        std::filesystem::path("hello.msg"), false);
    loaded.save_to_eml(std::filesystem::path("hello.eml"));

    std::cout << "Done\n";
}

步骤 3 — 构建

cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build

支持的格式

格式扩展名读取写入
MSG(Outlook 邮件).msg
EML(RFC 5322 / MIME).eml
CFB(复合文件二进制).msg, .cfb

开源与许可

Aspose.Email FOSS for C++ 在 MIT 许可证下发布。完整源码可在 GitHub 上的 aspose-email-foss/Aspose.Email-FOSS-for-Cpp 获取。商业使用无需版税。该库除了 C++17 编译器和 CMake 3.26 或更高版本外,没有运行时依赖。


入门