المقدمة

Aspose.Email FOSS للـ C++ هي مكتبة مفتوحة المصدر جديدة لقراءة وإنشاء وتحويل ملفات Outlook MSG، ورسائل RFC 5322 EML، وحاويات Compound File Binary (CFB) التي يُبنى عليها تنسيق MSG. تم كتابتها بلغة C++17، ولا تعتمد على أي تبعيات وقت تشغيل خارجية، وتُبنى على Linux و macOS و Windows عبر CMake — لا توجد تبعية COM ولا مسار شفرة خاص بمنصة معينة.

تظهر ملفات Outlook MSG باستمرار في أعمال تكامل المؤسسات — خطوط الأرشفة، أدوات الترحيل، تصديرات الامتثال — لكن قراءتها دون Outlook عادة ما كان يعني أتمتة COM على Windows أو مكتبة تجارية. 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) — تنسيق التخزين الهيكلي المستخدم أيضًا في صيغ Office الثنائية القديمة. 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، وقم بتحويلها ذهابًا وإيابًا إلى 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✓✓
Compound File Binary (CFB).cfb✓✓

المصدر المفتوح والترخيص

Aspose.Email FOSS للغة C++ مُصدرة تحت رخصة MIT. الشيفرة الكاملة متوفرة على aspose-email-foss/Aspose.Email-FOSS-for-Cpp، ويُسمح بالاستخدام التجاري دون حقوق ملكية. لا توجد تبعيات وقت تشغيل للمكتبة بخلاف مُترجم C++17 و CMake 3.26 أو أحدث.


البدء

الموارد ذات الصلة