MSG ファイルの読み取り
MSG ファイルの読み取り
mapi_message は Outlook MSG ファイルの読み込みと解析のエントリーポイントです。from_file() または from_stream() を一度呼び出すだけで、CFB コンテナ全体を読み取り、すべての MAPI プロパティを抽出し、型付きアクセサメソッドを通じて提供します — Outlook のインストールは不要です。
ファイル パスからロード
mapi_message::from_file() を std::filesystem::path と共に使用します:
#include <filesystem>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
const auto message = aspose::email::foss::msg::mapi_message::from_file(
std::filesystem::path("sample.msg"));
std::cout << "Subject: " << message.subject() << '\n';
std::cout << "From: " << message.sender_name()
<< " <" << message.sender_email_address() << ">\n";
std::cout << "Body: " << message.body() << '\n';
}ストリームからロード
MSG データがネットワーク ソケット、アーカイブ、または任意の std::istream から届く場合は mapi_message::from_stream() を使用します:
#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
std::ifstream input("sample.msg", std::ios::binary);
const auto message = aspose::email::foss::msg::mapi_message::from_stream(input);
std::cout << "Subject: " << message.subject() << '\n';
}ストリームは常に std::ios::binary で開いてください。テキストモードのストリームは Windows で改行を変換し、CFB コンテナを破損させます。
メッセージ プロパティにアクセス
mapi_message は、最も一般的なメッセージ プロパティを名前付きゲッターとして公開します:
#include <filesystem>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
const auto message = aspose::email::foss::msg::mapi_message::from_file(
std::filesystem::path("sample.msg"));
// Core properties
std::cout << "Subject: " << message.subject() << '\n';
std::cout << "Sender name: " << message.sender_name() << '\n';
std::cout << "Sender email: " << message.sender_email_address() << '\n';
std::cout << "Internet msg-id: " << message.internet_message_id() << '\n';
std::cout << "Message class: " << message.message_class() << '\n';
// Body — check html_body() first; fall back to plain body()
const auto& body = message.html_body().empty()
? message.body()
: message.html_body();
std::cout << "Body length: " << body.size() << " chars\n";
}受信者を列挙
message.recipients() は、display_name、email_address、および recipient_type を備えた受信者オブジェクトの std::vector を返します:
#include <filesystem>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
const auto message = aspose::email::foss::msg::mapi_message::from_file(
std::filesystem::path("sample.msg"));
for (std::size_t i = 0; i < message.recipients().size(); ++i)
{
const auto& r = message.recipients()[i];
std::cout << "[" << (i + 1) << "] "
<< r.display_name << " <" << r.email_address << ">"
<< " type=" << r.recipient_type << '\n';
}
}添付ファイルを列挙
message.attachments() は、添付オブジェクトの std::vector を返します。各オブジェクトは filename、mime_type、data、content_id、および is_embedded_message() を公開します:
#include <filesystem>
#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
const auto message = aspose::email::foss::msg::mapi_message::from_file(
std::filesystem::path("sample.msg"));
for (const auto& a : message.attachments())
{
std::cout << a.filename
<< " " << a.mime_type
<< " " << a.data.size() << " bytes"
<< " embedded=" << (a.is_embedded_message() ? "yes" : "no")
<< '\n';
if (!a.is_embedded_message())
{
std::ofstream out("extracted_" + a.filename, std::ios::binary);
out.write(reinterpret_cast<const char*>(a.data.data()),
static_cast<std::streamsize>(a.data.size()));
}
}
}埋め込みメッセージにアクセス
is_embedded_message() が true の場合、添付ファイルには別の MSG ファイルが含まれます。attachment.embedded_message を介してアクセスしてください:
#include <filesystem>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
const auto message = aspose::email::foss::msg::mapi_message::from_file(
std::filesystem::path("sample.msg"));
for (const auto& a : message.attachments())
{
if (a.is_embedded_message() && a.embedded_message != nullptr)
{
const auto& inner = *a.embedded_message;
std::cout << "Embedded subject: " << inner.subject() << '\n';
inner.save(std::filesystem::path("embedded.msg"));
}
}
}ヒントとベストプラクティス
- MSG ファイルを読み込む際は、常に
std::ios::binaryをstd::ifstreamに渡してください。 body()の前にhtml_body()を確認してください — HTML のみのメッセージは、内容を HTML の body プロパティに格納し、body()は空のままにします。from_file()はすべての添付データをメモリに読み込みます。大きな MSG ファイルの場合、msg_readerを使用して基盤となるコンテナを開き、添付バイトを完全に実体化せずにメタデータを検査することを検討してください。from_file(path, strict)とfrom_stream(stream, strict)のstrictパラメータはデフォルトでfalseになっており、実際の MSG ファイルにおける軽微な構造上の偏差を許容します。- 受信者オブジェクトは、To、Cc、Bcc エントリを区別するために
recipient_typeを公開します。
一般的な問題
| 問題 | 原因 | 修正 |
|---|---|---|
msg_exception が有効に見えるファイルで | ファイルは本物の OLE CFB コンテナではありません | cfb_reader::from_file() で検証 — それでも例外がスローされる場合、ファイルは有効な MSG ではありません |
body() が空文字列を返します | メッセージは HTML 本文のみを使用しています | 代わりに html_body() を使用してください |
recipients() は空です | 受信者テーブルのない古い MSG | get_property_value() 経由で display_to にアクセスし、common_message_property_id::display_to を使用する |
| Windows でのストリーム破損 | std::ios::binary なしでストリームが開かれました | std::ifstream に std::ios::binary フラグを追加する |
| 大きなファイルのメモリ使用量が高い | すべての添付データは事前にロードされます | 完全に具現化せずに低レベルアクセスするには msg_reader を使用します |
FAQ
from_file() はファイルハンドルを開いたままにしますか?
いいえ。ファイルは開かれ、完全に解析され、メソッドが戻る前に閉じられます。呼び出し後に元のファイルを移動または削除できます。
添付ファイルが通常のファイルか埋め込みMSGかをどうやって判断しますか?
attachment.is_embedded_message() を呼び出します。埋め込みMSGファイルの場合は true を返し、通常のファイル添付の場合は false を返します。埋め込みメッセージの場合は、attachment.embedded_message を介して入れ子オブジェクトにアクセスします。
from_file() と msg_reader を使用することの違いは何ですか?
mapi_message::from_file() は高レベルの API です — 完全なメッセージを解析し、プロパティを型付きフィールドとして公開します。msg_reader は低レベルのリーダーで、未加工のCFBコンテナ構造へのアクセスを提供し、すべてのMAPIプロパティを解析せずにメッセージのレイアウトを検査したい場合に便利です。
Unicode と ANSI の MSG ファイルは両方ともサポートされていますか?
はい。mapi_message は Unicode(プロパティタイプ ptyp_string)と ANSI(ptyp_string8)の文字列プロパティの両方を処理します。unicode_strings() は Unicode プロパティで保存されたメッセージに対して true を返します。
API Reference 概要
| クラス / メソッド | 説明 |
|---|---|
mapi_message::from_file(path) | std::filesystem::path から MSG ファイルをロードします |
mapi_message::from_file(path, strict) | strict モードオプションでロードします |
mapi_message::from_stream(stream) | MSG を std::istream からロード |
mapi_message::from_stream(stream, strict) | strict mode オプションを指定してストリームからロード |
mapi_message::subject() | メッセージの件名文字列を返す |
mapi_message::body() | プレーンテキスト本文を返す |
mapi_message::html_body() | HTML 本文を返す(存在しない場合は空) |
mapi_message::sender_name() | 送信者の表示名を返す |
mapi_message::sender_email_address() | 送信者のメールアドレスを返す |
mapi_message::internet_message_id() | RFC 2822 の Message-ID ヘッダーを返す |
mapi_message::message_class() | MAPI メッセージクラス文字列を返す |
mapi_message::recipients() | 受信者リストを返す |
mapi_message::attachments() | 添付ファイルのリストを返す |
mapi_attachment::is_embedded_message() | 添付が埋め込みMSGの場合はTrue |
mapi_message::unicode_strings() | メッセージがUnicode文字列プロパティを使用している場合はTrue |