はじめに

Aspose.Email FOSS for C++ は、Outlook MSG ファイル、RFC 5322 EML メッセージ、そして MSG 形式が基づく Compound File Binary (CFB) コンテナの読み取り・作成・変換を行う新しいオープンソースライブラリです。C++17 で記述され、外部ランタイム依存はなく、CMake を通じて Linux、macOS、Windows 上でビルドできます — COM 依存やプラットフォーム固有のコードパスはありません。

Outlook MSG ファイルは、アーカイブパイプライン、移行ツール、コンプライアンスエクスポートなど、エンタープライズ統合作業で常に出現します — しかし Outlook 自体がない状態でそれらを読むには、通常 Windows の COM 自動化か商用ライブラリが必要でした。Aspose.Email FOSS for C++ は、ポータブルな代替手段を提供します:日常的なメッセージの読み取りと作成のための高レベル mapi_message クラスに加え、必要に応じて基盤となるコンテナ形式に直接アクセスできる低レベルの msg_reader、msg_document、cfb_reader クラスです。

このライブラリは MIT ライセンスの下、C++17 ソースとして配布され、プロジェクトに CMake のサブディレクトリとして追加され、AsposeEmailFoss ターゲットを公開します — パッケージマネージャへの登録や事前ビルドバイナリは必要ありません。


主な機能

Outlook MSG ファイルの読み取り

.msg ファイルを msg_reader::from_file() または msg_reader::from_stream() で開きます。メッセージのストレージやストリームに直接アクセスするには、リーダーを msg_document::from_reader() 付きの msg_document でラップします。

#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() はそれを読み込みます。両方向とも件名、送信者、受信者、添付ファイルを保持するため、Outlook を使用せずにメッセージを MSG と EML の間でやり取りできます。

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 ファイルはコンパウンド ファイル バイナリ (CFB) コンテナ内に格納されます — これはレガシー Office バイナリ形式でも使用される構造化ストレージ形式です。cfb_reader は生のコンテナを開き、ディレクトリツリーを直接公開します:storage_ids() と stream_ids() がすべてのエントリを一覧表示し、directory_entry_count() が総数を報告し、header() がコンテナのセクターレイアウトを返します。これは、mapi_message API が表面化しない MAPI データが必要な場合に便利です。

#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 for C++ は MIT ライセンスの下でリリースされています。完全なソースは aspose-email-foss/Aspose.Email-FOSS-for-Cpp で入手可能で、商用利用はロイヤリティなしで許可されています。このライブラリは C++17 コンパイラと CMake 3.26 以降以外にランタイム依存関係がありません。


はじめに

関連リソース