はじめに
C++でOutlook MSGファイルを扱うことは、従来Windows専用のCOMオートメーションが必要でした
または、制限のあるライセンスを持つ重量級のサードパーティライブラリが必要でした。Aspose.Email FOSS for C++
これを変えます:純粋なC++17ライブラリで、外部依存がなく、COMもなく、
プラットフォーム固有のコードもありません。Linux、macOS、Windowsのいずれでも同一にビルドできます。
このライブラリはMITライセンスの下でリリースされ、GitHubを通じてソースとして配布されています。
あなたは
リポジトリをクローンし、CMakeのサブディレクトリとして追加することで統合します — パッケージマネージャへの登録は不要で、バイナリを事前にビルドする必要もありません。
この投稿では、コアワークフローとして、既存の MSG ファイルの読み取り、プロパティの検査、プログラムで新しいメッセージの作成、そして MSG と EML 形式間の往復を順に解説します。
主な機能
既存の MSG ファイルを読む
msg_reader を使用して .msg ファイルを開き、msg_document を使用してその構造化されたコンテンツにアクセスします。
両方のクラスは aspose::email::foss::msg 名前空間にあります。
#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"));
auto document = aef::msg::msg_document::from_reader(reader);
msg_reader には、std::istream から読み取るための from_stream オーバーロードもあり、ファイルがディスク上に存在しないサーバーサイドやパイプラインのコンテキストで便利です。
メッセージをプログラムで作成する
mapi_message::create は、件名と本文を持つ新しいインメモリメッセージを生成します。次に
送信者の詳細を設定し、受信者を追加し、バイナリまたはテキストのペイロードを添付してから保存できます。
#include "aspose/email/foss/msg/mapi_message.hpp"
namespace aef = aspose::email::foss;
auto msg = aef::msg::mapi_message::create(
"Quarterly status update",
"Hello team,\n\nPlease find the rollout summary attached.\n\nRegards");
msg.set_sender_name("Build Agent");
msg.set_sender_email_address("build.agent@example.com");
msg.add_recipient("alice@example.com", "Alice Example");
msg.save(std::filesystem::path("output.msg"));
MSG を EML に変換
mapi_message は save_to_eml メソッドを使用した直接的な MSG から EML への変換をサポートします。
EML 出力は RFC 5322 (MIME) に準拠しており、任意の標準メール
クライアントや処理パイプラインと互換性があります。
auto loaded = aef::msg::mapi_message::from_file(
std::filesystem::path("message.msg"), /*strict=*/false);
// Save as EML to a file
loaded.save_to_eml(std::filesystem::path("message.eml"));
// Or capture as bytes
std::vector<std::uint8_t> eml_bytes = loaded.save_to_eml();
EMLからロード
逆方向も同様に簡単です: load_from_eml は EML ファイルを読み取り、メモリ内に mapi_message を生成し、これを MSG に再保存できます。
auto from_eml = aef::msg::mapi_message::load_from_eml(
std::filesystem::path("message.eml"));
from_eml.save(std::filesystem::path("restored.msg"));
低レベル CFB アクセス
高度なユースケース—たとえば、高レベル API では公開されていない生の MAPI プロパティを読み取る場合—cfb_reader は MSG フォーマットの基盤となるコンパウンド ファイル バイナリ コンテナへの直接アクセスを提供します。
#include "aspose/email/foss/cfb/cfb_reader.hpp"
namespace aef = aspose::email::foss;
auto cfb = aef::cfb::cfb_reader::from_file(std::filesystem::path("message.msg"));
auto storage_ids = cfb.storage_ids();
auto stream_ids = cfb.stream_ids();
クイックスタート
ステップ 1 — クローンしてサブディレクトリとして追加
git clone https://github.com/aspose-email-foss/Aspose.Email-FOSS-for-Cpp.git
プロジェクト CMakeLists.txt で:
cmake_minimum_required(VERSION 3.26)
project(my_project)
add_subdirectory(Aspose.Email-FOSS-for-Cpp)
add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE AsposeEmailFoss::AsposeEmailFoss)
ステップ 2 — インクルードと使用
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
namespace aef = aspose::email::foss;
int main()
{
// Create a message
auto msg = aef::msg::mapi_message::create("Hello from C++", "Body text here");
msg.save(std::filesystem::path("hello.msg"));
// Round-trip to EML
auto loaded = aef::msg::mapi_message::from_file(
std::filesystem::path("hello.msg"), false);
loaded.save_to_eml(std::filesystem::path("hello.eml"));
std::cout << "Done\n";
}
ステップ 3 — ビルド
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
サポートされている形式
| フォーマット | 拡張子 | 読み取り | 書き込み |
|---|---|---|---|
| MSG (Outlook Message) | .msg | ✓ | ✓ |
| EML (RFC 5322 / MIME) | .eml | ✓ | ✓ |
| CFB (Compound File Binary) | .msg, .cfb | ✓ | ✓ |
オープンソースとライセンス
Aspose.Email FOSS for C++ は MIT ライセンスの下でリリースされています。完全なソースは GitHub の aspose-email-foss/Aspose.Email-FOSS-for-Cpp で入手可能です。商用利用はロイヤリティなしで許可されています。このライブラリは C++17 コンパイラと CMake 3.26 以降以外のランタイム依存関係はありません。