简介

本文介绍了使用 Aspose.Email FOSS(一个纯 C++17、MIT 许可证的库,且没有外部依赖和 COM)在 C++ 中读取、创建和转换 Outlook MSG 文件的核心工作流程。您将学习如何读取已有的 MSG 文件、检查其属性、以编程方式创建新邮件,以及在 MSG 与 EML 格式之间进行往返转换。

该库在 Linux、macOS 和 Windows 上的构建方式完全相同。它通过 GitHub 以源代码形式分发,您只需克隆仓库并将其添加为 CMake 子目录即可集成——无需注册任何包管理器。


关键特性

读取已有 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();

快速入门

第一步 — 克隆并添加为子目录

将仓库克隆到项目的源代码树或 vendor 目录中:

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

然后在你的项目 CMakeLists.txt 中使用 add_subdirectory 引用它:

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)

第二步 — 包含并使用

包含 mapi_message 头文件,并编写一个简单程序来创建 MSG 文件并将其转换为 EML:

#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";
}

第三步 — 构建

在项目根目录运行以下 CMake 命令以配置和编译:

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

支持的格式

格式扩展名读取写入Notes
MSG.msg✓✓Outlook 消息格式
EML.eml✓✓RFC 5322 / MIME 电子邮件格式

CFB 备注。 MSG 文件在内部以 Compound File Binary 格式存储。库在加载 .msg 文件;不需要单独的 CFB 格式类。


开源与许可

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


快速入门

相关资源