どうやってC++でファイルを読み取るか。
Aspose.Email FOSS for C++ lets you open and parse Outlook MSG files with a single 機能呼び出し. mapi_message::from_file() 和 mapi_message::from_stream() ロード THE 完全なメッセージ(主体、身体、送信者、受信者のもの、および付属物)を内蔵記憶に保存する。 Outlook や COM 依存性がないオブジェクト。.
STEP 1 - プロジェクトの設定
レポジトリをクローンし、あなたの CMakeLists.txt:
git clone https://github.com/aspose-email-foss/Aspose.Email-FOSS-for-Cpp.gitadd_subdirectory(Aspose.Email-FOSS-for-Cpp)
target_link_libraries(your_target PRIVATE AsposeEmailFoss::AsposeEmailFoss)STEP 2 - ヘッダーを含む
#include "aspose/email/foss/msg/mapi_message.hpp"この単一ヘッダーは、 mapi_message すべての支援者の方々が、 aspose::email::foss::msg 名スペース.
Step 3 — Load a message from a File Path ファイルパスからメッセージをロードする
利用 mapi_message::from_file() あなたがAを持っているとき 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';
}Step 4 — Load a Message from a Stream メッセージをストリームからロードする
利用 mapi_message::from_stream() パルシュ・オブ・アンド・ザ・ピース(Parse from Anyone) std::istream 役に立つ為 ネットワーク上またはメモリ内バッファから受信されたデータの処理: MSG data received over a network or from an in-memory buffer:
#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
std::ifstream input("sample.msg", std::ios::binary);
auto message = aspose::email::foss::msg::mapi_message::from_stream(input);
std::cout << "Subject: " << message.subject() << '\n';
}常に流れを開くこと std::ios::binary Windows ラインエンドを防ぐ方法 CFBコンテナの破壊から生じた翻訳。.
STEP5 - 受信者をイテラートする
message.recipients() 返すA std::vector 受信者対象の物件、それぞれ display_name, email_address,♪そして recipient_type フィールド:
#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
std::ifstream input("sample.msg", std::ios::binary);
auto message = aspose::email::foss::msg::mapi_message::from_stream(input);
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';
}
}STEP 6 - イテリアート アドレス
message.attachments() 返すA std::vector 各物件の付属品:それぞれの附属物体 展示 filename, mime_type, data (原発のバイト), content_id, and the is_embedded_message() 説教:
#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
std::ifstream input("sample.msg", std::ios::binary);
auto message = aspose::email::foss::msg::mapi_message::from_stream(input);
for (std::size_t i = 0; i < message.attachments().size(); ++i)
{
const auto& a = message.attachments()[i];
std::cout << "[" << (i + 1) << "] "
<< a.filename << " mime=" << a.mime_type
<< " bytes=" << a.data.size()
<< " embedded=" << (a.is_embedded_message() ? "yes" : "no")
<< '\n';
}
}共通の問題と修正
空っぽ body() 充電後。. いくつかのメッセージは、テキストをHTMLボディにのみ保存します。 資産チェック message.html_body() いつ message.body() 空の文字列を返します。.
ストリーム開放無し std::ios::binary. Windows では、テキストモード・ストリーム翻訳 CRLF シークレンス、バイナリ CFB データを静かに破壊. Always open MSG files with std::ios::binary.
msg_exception 見た目の正しいファイルに投げかけます。. ファイルが本物であることを確認する MSG (OLE Compound File) いくつかのエクスポートツールによって保存されたファイルは、単純テキストまたは MIME で保存される場合があります。 パラシュートできない mapi_message::from_file().
recipients() 空のベクターを返します。. 古いファイルは時には受信者を省略することもあります。 テーブルですが、ストアの受信者表示は、 display_to ホーム > アクセス > お問い合わせ message.get_property_value() 同 common_message_property_id::display_to 和 property_type_code::ptyp_string.
メモリに大きなデータを付ける。. たmapi_message::from_file() 和 from_stream() すべての添付バイトを含むメッセージ全体をロードします。大きなファイルの場合は、開く コンテナの下に、 msg_reader 完全無しのストリームをチェックする を物質化する。.
よくある質問
何が違いか(What is the difference between) from_file() 和 from_stream()?
両方とも同一の生産物を作る。 mapi_message オブジェクト. from_file() 受け入れA std::filesystem::path 内部でファイルを開く。. from_stream() 読書 from どんな std::istream 提供する場合 - MSG のデータが既にメモリまたは ソケットの上に到着。.
うんこ from_file() ファイルをアップロードした後も開く?
ファイルは開き、完全にメモリにパスされ、以前には閉じます。 from_file() 返信:呼び出し直後にソースファイルを移動または削除できます。.
macOS または Linux で Outlook によって作成された MSG ファイルを読み取ることができますか?
はい. MSG の形式(OLE Compound File + MAPI 属性)は、各ファイルに移行可能です。 オペレーティングシステム: Any .msg どのバージョンの Outlook で作成されたファイルも開くことができます。 同 mapi_message::from_file().
HTML 体をどう読むか。?
呼び出し message.html_body().メッセージが単にテキストの体で作成された場合、, 空の文字列を返します。. message.body() 単純テキストの体を返します。.
厳格なモードはデフォルト?
mapi_message::from_file(path, strict) 和 from_stream(stream, strict) 受け入れるA オプション strict ブーレイド! default is false,マイナーな構造を許容するもの。 現実世界のファイルに存在する。.