מבוא

Aspose.Email FOSS for 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 for 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() כותב את ההודעה הנוכחית כקובץ 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() — יחד עם 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✓✓
קובץ בינארי משולב (CFB).cfb✓✓

קוד פתוח ורישוי

Aspose.Email FOSS עבור C++ משוחרר ברישיון MIT. הקוד המלא זמין ב-aspose-email-foss/Aspose.Email-FOSS-for-Cpp, ושימוש מסחרי מותר ללא תמלוגים. הספרייה אינה תלויה בזמן ריצה מעבר למקמפל C++17 ו-CMake 3.26 או גרסה מאוחרת יותר.


התחלה

משאבים קשורים