Giới thiệu
Aspose.Email FOSS cho C++ là một thư viện mã nguồn mở mới dành cho việc đọc, tạo và chuyển đổi các tệp Outlook MSG, các tin nhắn RFC 5322 EML, và các container Compound File Binary (CFB) mà định dạng MSG dựa trên. Nó được viết bằng C++17, không có phụ thuộc thời gian chạy bên ngoài, và có thể biên dịch trên Linux, macOS và Windows thông qua CMake — không có phụ thuộc COM và không có đường dẫn mã phụ thuộc nền tảng.
Các tệp Outlook MSG xuất hiện liên tục trong công việc tích hợp doanh nghiệp — các pipeline lưu trữ, công cụ di chuyển, xuất khẩu tuân thủ — nhưng việc đọc chúng mà không có Outlook thường đòi hỏi tự động hoá COM trên Windows hoặc một thư viện thương mại. Aspose.Email FOSS cho C++ cung cấp cho bạn một lựa chọn di động: một lớp mapi_message cấp cao cho việc đọc và tạo tin nhắn hàng ngày, cùng với các lớp msg_reader, msg_document và cfb_reader cấp thấp hơn để truy cập trực tiếp vào định dạng container nền tảng khi cần.
Thư viện được phân phối dưới dạng mã nguồn C++17 theo giấy phép MIT và được thêm vào dự án như một thư mục con CMake, tiết lộ mục tiêu AsposeEmailFoss — không cần đăng ký trình quản lý gói và không cần các binary đã biên dịch trước.
Các tính năng chính
Đọc các tệp Outlook MSG
Mở một tệp .msg bằng msg_reader::from_file() hoặc msg_reader::from_stream(). Để truy cập trực tiếp vào các kho lưu trữ và luồng của một tin nhắn, bao bọc trình đọc trong một msg_document với msg_document::from_reader().
#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);
Tạo Tin Nhắn Theo Chương Trình
mapi_message::create() tạo một tin nhắn mới trong bộ nhớ từ tiêu đề và nội dung. Đặt người gửi, thêm người nhận và đính kèm dữ liệu nhị phân hoặc văn bản trước khi lưu bằng 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"));
Chuyển Đổi Giữa MSG và EML
mapi_message::save_to_eml() ghi tin nhắn hiện tại dưới dạng tệp EML theo chuẩn RFC 5322, và mapi_message::load_from_eml() đọc lại tệp đó. Cả hai hướng đều bảo toàn tiêu đề, người gửi, người nhận và tệp đính kèm, vì vậy một tin nhắn có thể chuyển đổi giữa MSG và EML mà không cần 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"));
Kiểm Tra Thuộc Tính và Tệp Đính Kèm Của Tin Nhắn
Một mapi_message đã tải sẽ hiển thị các trường tiêu đề chuẩn của nó một cách trực tiếp — subject(), sender_name(), sender_email_address(), message_class(), và internet_message_id() — cùng với recipients() và attachments() dưới dạng các vector của mapi_recipient và mapi_attachment. Đối với các trường MAPI không có bộ truy cập riêng, get_property_value() đọc bất kỳ thuộc tính nào bằng common_message_property_id và 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));
Truy Cập Cấp Độ Thấp Đối Với Container CFB
Các tệp MSG được lưu trong một container Compound File Binary (CFB) — định dạng lưu trữ có cấu trúc cũng được sử dụng bởi các định dạng nhị phân Office cũ. cfb_reader mở container thô và hiển thị cây thư mục của nó một cách trực tiếp: storage_ids() và stream_ids() liệt kê mọi mục, directory_entry_count() báo cáo tổng số, và header() trả về bố trí sector của container. Điều này hữu ích khi bạn cần dữ liệu MAPI mà mapi_message API không hiển thị.
#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';
Bắt đầu nhanh
Thêm thư viện vào dự án của bạ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)Sau đó tạo một tin nhắn, lưu nó dưới dạng MSG, và chuyển đổi qua lại thành 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;
}
Định dạng được hỗ trợ
| Định dạng | Tiện ích mở rộng | Đọc | Ghi |
|---|---|---|---|
| Tin nhắn Outlook (MSG) | .msg | ✓ | ✓ |
| EML (RFC 5322 / MIME) | .eml | ✓ | ✓ |
| Tệp nhị phân hợp chất (CFB) | .cfb | ✓ | ✓ |
Mã nguồn mở & Giấy phép
Aspose.Email FOSS cho C++ được phát hành dưới giấy phép MIT. Mã nguồn đầy đủ có sẵn tại aspose-email-foss/Aspose.Email-FOSS-for-Cpp, và việc sử dụng thương mại được phép mà không có bản quyền. Thư viện không có bất kỳ phụ thuộc runtime nào ngoài một trình biên dịch C++17 và CMake 3.26 trở lên.