Aspose.Email FOSS for C++ 是一个 MIT-licensed、开源的 C++ 库,用于处理 Microsoft Outlook .msg 文件和 Compound File Binary (CFB) 容器。通过 CMake 包含头文件,即可立即开始读取、创建和处理电子邮件,而无需安装 Microsoft Outlook 或任何专有运行时。
该库提供两个访问层级。低层级中,cfb_reader 和 cfb_writer 提供对 CFB 二进制容器的完整控制——遍历目录条目,读取和写入存储节点及流数据,并检查原始扇区布局。msg_reader 和 msg_writer 在 CFB 之上处理 MSG 格式,公开 MAPI 属性流、收件人表和附件子存储。高层级中,mapi_message 允许您从头创建新消息,读取主题、正文、收件人和附件,并在 MSG 与 EML 格式之间进行转换。
该库可在任何配备 C++17 编译器的平台上构建,且没有外部依赖,使其适用于 Windows、Linux、macOS、Docker 容器和嵌入式系统。
提供了包含附加功能的企业版,标记为 Aspose.Email for C++。
cfb_reader::from_file()、cfb_reader::from_stream() 或 cfb_reader::from_bytes() 从文件路径、流或字节数组加载现有 CFB 容器。storage_ids()、stream_ids() 和 child_ids() 枚举存储和流,并使用 resolve_path() 导航嵌套层次结构。cfb_node::is_storage() 和 cfb_node::is_stream() 检查节点类型,通过 creation_time() 和 modified_time() 读取时间戳。cfb_document 以编程方式构建 CFB 文档,并通过 cfb_writer::to_bytes() 或 cfb_writer::write_file() 将其序列化为字节或文件。.msg 文件的容器格式 — 直接访问 CFB 可实现取证检查和修复。msg_reader::from_file() 或 msg_reader::from_stream() 打开 Outlook MSG 文件,并访问底层的 MAPI 属性流和附件子存储。msg_writer::to_bytes() 或 msg_writer::write_file() 从 msg_document 生成 MSG 文件。msg_document::major_version()、msg_document::minor_version() 访问版本元数据,并使用 msg_document::strict() 检查严格性。msg_document::to_cfb_document() 提取底层 CFB 文档。.msg 文件从归档目录并提取元数据或附件。mapi_message::create() 构建完整的电子邮件,然后通过 set_subject()、set_body() 和 set_html_body() 设置主题、正文和 HTML 正文。set_sender_name()、set_sender_email_address() 和 set_sender_address_type()。mapi_attachment::from_bytes() 和 mapi_attachment::from_stream() 从字节或流加载附件,并通过 is_embedded_message() 检查嵌入的消息。mapi_message::save() 序列化,使用 mapi_message::from_file() 或 mapi_message::from_stream() 重新加载。.eml 文件(RFC 5322 / MIME)解析为完整的 mapi_message 对象,通过 mapi_message::load_from_eml()。mapi_message 序列化回 MIME 格式,使用 save_to_eml()。.eml 和 .msg 文件的混合归档规范化为单一格式。从流中打开 Outlook MSG 文件并打印主题 — 无需 Microsoft Outlook。
#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
std::ifstream input("sample.msg", std::ios::binary);
auto message = aspose::email::foss::msg::mapi_message::from_stream(input);
std::cout << message.subject() << '\n';
}
构建一个包含发件人、收件人和附件的完整电子邮件,然后将其写入 MSG 和 EML 格式。
#include <fstream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
auto message = aspose::email::foss::msg::mapi_message::create("Hello", "Body");
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");
std::ofstream msg_output("hello.msg", std::ios::binary);
message.save(msg_output);
std::ofstream eml_output("hello.eml", std::ios::binary);
message.save_to_eml(eml_output);
}