介绍
Aspose.Email FOSS for C++ 是一个新的开源库,用于读取、创建和转换 Outlook MSG 文件、RFC 5322 EML 消息以及 MSG 格式所基于的复合文件二进制(CFB)容器。它使用 C++17 编写,无外部运行时依赖,并通过 CMake 在 Linux、macOS 和 Windows 上构建——没有 COM 依赖,也没有平台特定的代码路径。
Outlook MSG 文件在企业集成工作中频繁出现——归档流水线、迁移工具、合规导出——但若不使用 Outlook 本身,读取它们通常需要在 Windows 上进行 COM 自动化或使用商业库。Aspose.Email FOSS for C++ 为您提供了可移植的替代方案:一个用于日常消息读取和创建的高级 mapi_message 类,以及在需要时直接访问底层容器格式的低层 msg_reader、msg_document 和 cfb_reader 类。
该库以 MIT 许可证下的 C++17 源代码形式分发,并作为 CMake 子目录添加到项目中,公开 AsposeEmailFoss 目标——无需包管理器注册,也不需要预构建二进制文件。
关键特性
读取 Outlook MSG 文件
使用 msg_reader::from_file() 或 msg_reader::from_stream() 打开一个 .msg 文件。若要直接访问消息的存储和流,请使用 msg_document::from_reader() 将读取器包装在 msg_document 中。
#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"), /*strict=*/false);
auto document = aef::msg::msg_document::from_reader(reader);
以编程方式创建消息
mapi_message::create() 从主题和正文构建一个新的内存消息。设置发件人,添加收件人,并在使用 save() 保存之前附加二进制或文本负载。
#include "aspose/email/foss/msg/mapi_message.hpp"
#include <filesystem>
namespace aef = aspose::email::foss;
auto message = aef::msg::mapi_message::create(
"Quarterly status update", "Hello team,\n\nRollout summary attached.");
message.set_sender_name("Build Agent");
message.set_sender_email_address("build.agent@example.com");
message.add_recipient("alice@example.com", "Alice Example");
message.add_recipient("carol@example.com", "Carol Example",
aef::msg::mapi_message::recipient_type_cc);
message.add_attachment("report.txt", std::vector<std::uint8_t>{'o', 'k'}, "text/plain");
message.save(std::filesystem::path("output.msg"));
在 MSG 与 EML 之间转换
mapi_message::save_to_eml() 将当前消息写入 RFC 5322 EML 文件,而 mapi_message::load_from_eml() 则读取该文件。两个方向都保留主题、发件人、收件人和附件,因此消息可以在 MSG 与 EML 之间转移而无需 Outlook。
auto message = aef::msg::mapi_message::from_file(
std::filesystem::path("message.msg"), /*strict=*/false);
message.save_to_eml(std::filesystem::path("message.eml"));
auto restored = aef::msg::mapi_message::load_from_eml(std::filesystem::path("message.eml"));
restored.save(std::filesystem::path("restored.msg"));
检查消息属性和附件
已加载的 mapi_message 直接公开其标准头字段——subject()、sender_name()、sender_email_address()、message_class() 和 internet_message_id()——以及作为 mapi_recipient 与 mapi_attachment 向量的 recipients() 和 attachments()。对于没有专用访问器的 MAPI 字段,get_property_value() 可通过其 common_message_property_id 和 property_type_code 读取任何属性。
#include "aspose/email/foss/msg/mapi_message.hpp"
#include "aspose/email/foss/msg/common_message_property_id.hpp"
#include "aspose/email/foss/msg/property_type_code.hpp"
#include <iostream>
namespace aef = aspose::email::foss;
template <typename E>
constexpr auto to_underlying(E value) noexcept {
return static_cast<std::underlying_type_t<E>>(value);
}
auto message = aef::msg::mapi_message::from_file(
std::filesystem::path("message.msg"), /*strict=*/false);
std::cout << "Subject: " << message.subject() << '\n';
std::cout << "From: " << message.sender_name()
<< " <" << message.sender_email_address() << ">\n";
std::cout << "Recipients: " << message.recipients().size() << '\n';
for (const auto& attachment : message.attachments()) {
std::cout << "- " << attachment.filename
<< " (" << attachment.mime_type << ", "
<< attachment.data.size() << " bytes)\n";
}
const auto display_cc = message.get_property_value(
to_underlying(aef::msg::common_message_property_id::display_cc),
to_underlying(aef::msg::property_type_code::ptyp_string));
低层 CFB 容器访问
MSG 文件存储在复合文件二进制(CFB)容器中——这种结构化存储格式也用于旧版 Office 二进制格式。cfb_reader 打开原始容器并直接公开其目录树:storage_ids() 和 stream_ids() 列出所有条目,directory_entry_count() 报告总数,header() 返回容器的扇区布局。当您需要 mapi_message API 未显示的 MAPI 数据时,这非常有用。
#include "aspose/email/foss/cfb/cfb_reader.hpp"
#include <iostream>
namespace aef = aspose::email::foss;
auto cfb = aef::cfb::cfb_reader::from_file(std::filesystem::path("message.msg"));
std::cout << "Directory entries: " << cfb.directory_entry_count() << '\n';
std::cout << "Storages: " << cfb.storage_ids().size() << '\n';
std::cout << "Streams: " << cfb.stream_ids().size() << '\n';
快速入门
将库添加到您的项目中:
git clone https://github.com/aspose-email-foss/Aspose.Email-FOSS-for-Cpp.git# CMakeLists.txt
add_subdirectory(Aspose.Email-FOSS-for-Cpp)
target_link_libraries(your_target PRIVATE AsposeEmailFoss::AsposeEmailFoss)然后创建一条消息,将其保存为 MSG,并将其往返转换为 EML:
#include "aspose/email/foss/msg/mapi_message.hpp"
#include <filesystem>
#include <vector>
#include <cstdint>
namespace aef = aspose::email::foss;
int main() {
auto message = aef::msg::mapi_message::create("Hello from C++", "Body text here");
message.set_sender_name("Alice");
message.set_sender_email_address("alice@example.com");
message.add_recipient("bob@example.com", "Bob");
message.add_attachment("note.txt", std::vector<std::uint8_t>{'a', 'b', 'c'}, "text/plain");
message.save(std::filesystem::path("hello.msg"));
auto loaded = aef::msg::mapi_message::from_file(
std::filesystem::path("hello.msg"), /*strict=*/false);
loaded.save_to_eml(std::filesystem::path("hello.eml"));
return 0;
}
支持的格式
| 格式 | 扩展名 | 读取 | 写入 |
|---|---|---|---|
| Outlook 消息 (MSG) | .msg | ✓ | ✓ |
| EML (RFC 5322 / MIME) | .eml | ✓ | ✓ |
| 复合文件二进制 (CFB) | .cfb | ✓ | ✓ |
开源与许可
Aspose.Email FOSS for C++ 在 MIT 许可证下发布。完整源码可在 aspose-email-foss/Aspose.Email-FOSS-for-Cpp 获取,商业使用无需版税。该库除了 C++17 编译器和 CMake 3.26 或更高版本外,没有运行时依赖。