مقدمه
Aspose.Email FOSS برای C++ یک کتابخانهٔ متنباز جدید برای خواندن، ایجاد و تبدیل فایلهای Outlook MSG، پیامهای RFC 5322 EML، و کانتینرهای Compound File Binary (CFB) که فرمت MSG بر پایهٔ آن ساخته شده است، میباشد. این کتابخانه با C++17 نوشته شده است، هیچ وابستگی زمان اجرا خارجی ندارد، و از طریق CMake بر روی Linux، macOS و Windows ساخته میشود — هیچ وابستگی COM و هیچ مسیر کد مخصوص پلتفرم وجود ندارد.
فایلهای Outlook MSG بهطور مداوم در کارهای یکپارچهسازی سازمانی ظاهر میشوند — خطوط لوله بایگانی، ابزارهای مهاجرت، صادرات انطباقی — اما خواندن آنها بدون خود Outlook معمولاً به معنای خودکارسازی COM در ویندوز یا استفاده از کتابخانهٔ تجاری بوده است. Aspose.Email FOSS برای C++ یک جایگزین قابل حمل به شما میدهد: یک کلاس سطح بالا mapi_message برای خواندن و ایجاد پیامهای روزمره، بهعلاوه کلاسهای سطح پایین msg_reader، msg_document و cfb_reader برای دسترسی مستقیم به قالب زیرین کانتینر در صورت نیاز.
این کتابخانه بهعنوان منبع C++17 تحت مجوز MIT توزیع میشود و بهعنوان یک زیرپوشهٔ CMake به پروژه اضافه میشود، هدف AsposeEmailFoss را افشا میکند — نیازی به ثبت در مدیر بسته و هیچ باینری پیشساختهای نیست.
ویژگیهای کلیدی
خواندن فایلهای Outlook MSG
یک فایل .msg را با msg_reader::from_file() یا msg_reader::from_stream() باز کنید. برای دسترسی مستقیم به ذخیرهسازیها و جریانهای یک پیام، خواننده را در یک msg_document با 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);
ایجاد پیامها بهصورت برنامهنویسی
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() پیام فعلی را بهعنوان یک فایل EML مطابق RFC 5322 مینویسد و 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() — همراه با recipients() و attachments() بهعنوان بردارهایی از mapi_recipient و mapi_attachment. برای فیلدهای 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 درون یک مخزن Compound File Binary (CFB) ذخیره میشوند — فرمت ذخیرهسازی ساختاری که توسط فرمتهای باینری قدیمی آفیس نیز استفاده میشود. cfb_reader مخزن خام را باز میکند و درخت دایرکتوری آن را بهصورت مستقیم نشان میدهد: storage_ids() و stream_ids() هر ورودی را فهرست میکنند، directory_entry_count() کل تعداد را گزارش میدهد، و header() طرحبندی سکترهای مخزن را برمیگرداند. این زمانی مفید است که به دادههای MAPI نیاز دارید که mapi_message API آشکار نمیکند.
#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 ذخیره کنید و به صورت round-trip به 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 برای C++ تحت مجوز MIT منتشر شده است. کد منبع کامل در aspose-email-foss/Aspose.Email-FOSS-for-Cpp در دسترس است، و استفاده تجاری بدون حق امتیاز مجاز است. این کتابخانه هیچ وابستگی زمان اجرا دیگری جز یک کامپایلر C++17 و CMake 3.26 یا بالاتر ندارد.