介绍

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

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

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


关键特性

读取现有 MSG 文件

使用 msg_reader 打开 .msg 文件,并使用 msg_document 访问其结构化内容。两个 classes 位于 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 还具有一个用于从 std::istream 读取的 from_stream 重载,这在服务器端或管道环境中文件不在磁盘上时非常有用。

以编程方式创建消息

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 格式的 Compound File Binary 容器的直接访问。

#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 (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 或更高版本外,没有运行时依赖。


入门指南

相关资源