Wprowadzenie

Aspose.Email FOSS for C++ to nowa biblioteka open-source służąca do odczytu, tworzenia i konwertowania plików Outlook MSG, wiadomości RFC 5322 EML oraz kontenerów Compound File Binary (CFB), na których opiera się format MSG. Jest napisana w C++17, nie ma zewnętrznych zależności uruchomieniowych i kompiluje się na Linux, macOS i Windows przy użyciu CMake — nie wymaga zależności COM ani platformowo-specyficznych ścieżek kodu.

Pliki Outlook MSG pojawiają się nieustannie w pracy integracyjnej przedsiębiorstw — w pipeline’ach archiwizacji, narzędziach migracji, eksportach zgodności — ale ich odczyt bez samego Outlooka zwykle wymagał automatyzacji COM w systemie Windows lub komercyjnej biblioteki. Aspose.Email FOSS for C++ oferuje przenośną alternatywę: wysokopoziomową klasę mapi_message do codziennego odczytu i tworzenia wiadomości, a także niżej-poziomowe klasy msg_reader, msg_document i cfb_reader zapewniające bezpośredni dostęp do podstawowego formatu kontenera, gdy jest to potrzebne.

Biblioteka jest dystrybuowana jako źródło C++17 na licencji MIT i dodawana do projektu jako podkatalog CMake, udostępniając cel AsposeEmailFoss — nie wymaga rejestracji w menedżerze pakietów ani prekompilowanych binarek.


Kluczowe funkcje

Odczyt plików Outlook MSG

Otwórz plik .msg przy użyciu msg_reader::from_file() lub msg_reader::from_stream(). Aby uzyskać bezpośredni dostęp do magazynów i strumieni wiadomości, opakuj czytnik w msg_document z 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);

Tworzenie wiadomości programowo

mapi_message::create() tworzy nową wiadomość w pamięci na podstawie tematu i treści. Ustaw nadawcę, dodaj odbiorców i dołącz binarne lub tekstowe ładunki przed zapisaniem przy użyciu 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"));

Konwersja między MSG a EML

mapi_message::save_to_eml() zapisuje bieżącą wiadomość jako plik EML zgodny z RFC 5322, a mapi_message::load_from_eml() odczytuje go z powrotem. Obie operacje zachowują temat, nadawcę, odbiorców i załączniki, dzięki czemu wiadomość może przemieszczać się między MSG a EML bez Outlooka.

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"));

Badanie właściwości wiadomości i załączników

Załadowany mapi_message udostępnia swoje standardowe pola nagłówka bezpośrednio — subject(), sender_name(), sender_email_address(), message_class() i internet_message_id() — oraz recipients() i attachments() jako wektory mapi_recipient i mapi_attachment. Dla pól MAPI bez dedykowanego akcesora, get_property_value() odczytuje dowolną właściwość po jej common_message_property_id i 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));

Niskopoziomowy dostęp do kontenera CFB

Pliki MSG są przechowywane wewnątrz kontenera Compound File Binary (CFB) — formatu strukturalnego magazynu używanego również przez starsze binarne formaty Office. cfb_reader otwiera surowy kontener i udostępnia jego drzewo katalogów bezpośrednio: storage_ids() i stream_ids() wymieniają wszystkie wpisy, directory_entry_count() podaje ich liczbę, a header() zwraca układ sektorów kontenera. Jest to przydatne, gdy potrzebujesz danych MAPI, których mapi_message API nie udostępnia.

#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';

Szybki start

Dodaj bibliotekę do swojego projektu:

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)

Następnie utwórz wiadomość, zapisz ją jako MSG i wykonaj konwersję do 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;
}

Obsługiwane formaty

FormatRozszerzenieOdczytZapis
Wiadomość Outlook (MSG).msg✓✓
EML (RFC 5322 / MIME).eml✓✓
Compound File Binary (CFB).cfb✓✓

Open Source i licencjonowanie

Aspose.Email FOSS dla C++ jest udostępniony na licencji MIT. Pełne źródła są dostępne pod adresem aspose-email-foss/Aspose.Email-FOSS-for-Cpp, a komercyjne użycie jest dozwolone bez opłat licencyjnych. Biblioteka nie ma żadnych zależności uruchomieniowych poza kompilatorem C++17 i CMake 3.26 lub nowszym.


Rozpoczęcie

Powiązane zasoby