Einleitung
Aspose.Email FOSS für C++ ist eine neue Open-Source-Bibliothek zum Lesen, Erstellen und Konvertieren von Outlook-MSG-Dateien, RFC-5322-EML-Nachrichten und den Compound-File-Binary-(CFB-)Containern, auf denen das MSG-Format basiert. Sie ist in C++17 geschrieben, hat keine externen Laufzeitabhängigkeiten und lässt sich unter Linux, macOS und Windows mittels CMake bauen — es gibt keine COM-Abhängigkeit und keinen plattformspezifischen Codepfad.
Outlook-MSG-Dateien tauchen in der Unternehmensintegration ständig auf — Archivierungspipelines, Migrationstools, Compliance-Exporte — doch das Auslesen ohne Outlook selbst bedeutete meist COM-Automatisierung unter Windows oder eine kommerzielle Bibliothek. Aspose.Email FOSS für C++ bietet Ihnen eine portable Alternative: eine High-Level-mapi_message-Klasse für das tägliche Lesen und Erstellen von Nachrichten sowie niedrigere msg_reader-, msg_document- und cfb_reader-Klassen für den direkten Zugriff auf das zugrunde liegende Container-Format, wenn Sie es benötigen.
Die Bibliothek wird als C++17-Quellcode unter der MIT-Lizenz verteilt und einem Projekt als CMake-Unterverzeichnis hinzugefügt, wobei das AsposeEmailFoss-Target bereitgestellt wird — eine Registrierung bei einem Paketmanager und vorgefertigte Binärdateien sind nicht erforderlich.
Wichtige Funktionen
Outlook-MSG-Dateien lesen
Öffnen Sie eine .msg-Datei mit msg_reader::from_file() oder msg_reader::from_stream(). Um direkt auf die Speicher und Streams einer Nachricht zuzugreifen, wickeln Sie den Reader in ein msg_document mit 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);
Nachrichten programmgesteuert erstellen
mapi_message::create() erstellt eine neue In-Memory-Nachricht aus Betreff und Text. Setzen Sie den Absender, fügen Sie Empfänger hinzu und hängen Sie binäre oder Text-Payloads an, bevor Sie mit save() speichern.
#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"));
Konvertieren zwischen MSG und EML
mapi_message::save_to_eml() schreibt die aktuelle Nachricht als RFC-5322-EML-Datei, und mapi_message::load_from_eml() liest sie wieder ein. Beide Richtungen erhalten Betreff, Absender, Empfänger und Anhänge, sodass eine Nachricht zwischen MSG und EML ohne Outlook übertragen werden kann.
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"));
Nachrichteneigenschaften und Anhänge inspizieren
Ein geladenes mapi_message stellt seine Standard-Header-Felder direkt zur Verfügung — subject(), sender_name(), sender_email_address(), message_class() und internet_message_id() — sowie recipients() und attachments() als Vektoren von mapi_recipient bzw. mapi_attachment. Für MAPI-Felder ohne dedizierten Zugriff gibt get_property_value() jede Eigenschaft über ihr common_message_property_id und property_type_code aus.
#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));
Low-Level-Zugriff auf CFB-Container
MSG-Dateien werden innerhalb eines Compound File Binary (CFB)-Containers gespeichert — dem Structured-Storage-Format, das auch von alten Office-Binärformaten verwendet wird. cfb_reader öffnet den rohen Container und stellt dessen Verzeichnisbaum direkt zur Verfügung: storage_ids() und stream_ids() listen jeden Eintrag auf, directory_entry_count() gibt die Gesamtzahl aus, und header() liefert das Sektor-Layout des Containers. Das ist nützlich, wenn Sie MAPI-Daten benötigen, die das mapi_message API nicht bereitstellt.
#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';
Schnellstart
Fügen Sie die Bibliothek zu Ihrem Projekt hinzu:
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)Erstellen Sie dann eine Nachricht, speichern Sie sie als MSG und führen Sie einen Round-Trip zu EML durch:
#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;
}
Unterstützte Formate
| Format | Erweiterung | Lesen | Schreiben |
|---|---|---|---|
| Outlook-Nachricht (MSG) | .msg | ✓ | ✓ |
| EML (RFC 5322 / MIME) | .eml | ✓ | ✓ |
| Compound File Binary (CFB) | .cfb | ✓ | ✓ |
Open Source & Lizenzierung
Aspose.Email FOSS für C++ wird unter der MIT-Lizenz veröffentlicht. Der vollständige Quellcode ist verfügbar unter aspose-email-foss/Aspose.Email-FOSS-for-Cpp, und die kommerzielle Nutzung ist ohne Lizenzgebühren gestattet. Die Bibliothek hat keine Laufzeitabhängigkeiten außer einem C++17-Compiler und CMake 3.26 oder höher.