どうやって C++で添付ファイルを操作するか
Aspose.Email FOSS for C++ provides the mapi_attachment 添付を作成するためのクラス 原発や流れから、そして mapi_message 読み込み添付書の追加リスト 既存の MSG ファイルから. メッセージを編集する際に添付ファイルを追加し、それらを確認できます。 ファイルをロードするとき、附属書自体が埋め込まれたものかどうかを検出することなど メッセージの仕方。.
STEP 1 - プロジェクトの設定
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 - バイトからファイル添付を追加する
利用 mapi_message::add_attachment() メッセージを構成する際にバイナリデータを付加する。. ファイル名を提供し、 Raw Byte を A として指定します。 std::vector<std::uint8_t>,しかも、その場の コンテンツタイプ:
#include <filesystem>
#include <vector>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
auto message = aspose::email::foss::msg::mapi_message::create(
"Report", "Please review the attached report.");
message.set_sender_name("Alice");
message.set_sender_email_address("alice@example.com");
message.add_recipient("bob@example.com", "Bob");
// Attach a text file
std::vector<std::uint8_t> text_data{'H', 'e', 'l', 'l', 'o'};
message.add_attachment("notes.txt", text_data, "text/plain");
// Attach a binary blob
std::vector<std::uint8_t> bin_data{0x00, 0x01, 0x02, 0x03};
message.add_attachment("data.bin", bin_data, "application/octet-stream");
message.save(std::filesystem::path("report.msg"));
}Step 3 — Add an attachment from a file on Disk ディスクのファイルを追加する
ファイルをバイトベクターに読み込み、それからそれを転送します。 add_attachment():
#include <filesystem>
#include <fstream>
#include <iterator>
#include <vector>
#include "aspose/email/foss/msg/mapi_message.hpp"
int main()
{
// Read a file into bytes
std::ifstream file("document.pdf", std::ios::binary);
std::vector<std::uint8_t> pdf_bytes(
std::istreambuf_iterator<char>(file),
std::istreambuf_iterator<char>{});
auto message = aspose::email::foss::msg::mapi_message::create(
"Document", "See attached PDF.");
message.set_sender_name("Alice");
message.set_sender_email_address("alice@example.com");
message.add_recipient("bob@example.com", "Bob");
message.add_attachment("document.pdf", pdf_bytes, "application/pdf");
message.save(std::filesystem::path("with_pdf.msg"));
}Step 4 — Read attachments from an existing MSG file. 既存のファイルから添付を読み取る
message.attachments() 返すA const 接続物体のベクターを参照する。. 各展示 filename, mime_type, data (原発のバイト)と、 content_id:
#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 << "Attachments: " << message.attachments().size() << '\n';
for (std::size_t i = 0; i < message.attachments().size(); ++i)
{
const auto& a = message.attachments()[i];
std::cout << "[" << (i + 1) << "]"
<< " name=" << a.filename
<< " mime=" << a.mime_type
<< " bytes=" << a.data.size()
<< " content_id=" << a.content_id
<< '\n';
}
}Step 5 - インベッドメッセージの検出とアクセス
いくつかの MSG ファイルには、他の MSG のファイルが添付ファイルとして含まれています(送信または会議招待状)。. 利用 is_embedded_message() これらを検出し、ネストされたメッセージにアクセスするために embedded_message:
#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);
for (const auto& attachment : message.attachments())
{
if (attachment.is_embedded_message() && attachment.embedded_message != nullptr)
{
const auto& inner = *attachment.embedded_message;
std::cout << "Embedded subject: " << inner.subject() << '\n';
std::cout << "Embedded from: " << inner.sender_email_address() << '\n';
// Save the embedded message as a standalone MSG
inner.save(std::filesystem::path("embedded.msg"));
}
else
{
std::cout << "Regular attachment: " << attachment.filename
<< " (" << attachment.data.size() << " bytes)\n";
}
}
}STEP 6 - ディスクに付属データを保存する
添付バイトを抽出し、標準の C++ ファイルストリームを使用してファイルに書き込む:
#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"));
const std::filesystem::path out_dir("attachments");
std::filesystem::create_directories(out_dir);
for (const auto& a : message.attachments())
{
if (!a.is_embedded_message())
{
const auto out_path = out_dir / a.filename;
std::ofstream out(out_path, std::ios::binary);
out.write(reinterpret_cast<const char*>(a.data.data()),
static_cast<std::streamsize>(a.data.size()));
std::cout << "Saved: " << out_path << '\n';
}
}
}共通の問題と修正
attachment.data 知恵の豊かな空き地にあります。. ファイルの源を確認する 実際には、添付データ(OLEリンクではなく)が含まれています。 MSGファイルは一部のファイルから輸出されています。 メール クライアントは、図書館が追跡できない外部の参照として付属ファイルを保存する場合があります。.
is_embedded_message() 返り方 true だが、 embedded_message は nullptr. ザ・ 入力したデータを表示する、ファイルヘッダーは MSG が組み込まれていることを示していますが、データは読み取れないようにしました。 源は、有効で、割り切れていない。.
抽出した添付ファイルを保存すると、パスエラーが発生しない。. いくつかのファイル名 ターゲットファイルシステムで無効な文字を含む(例えば、., :, ? オイ ウィンドウズ(衛生) attachment.filename 出力の道を築く前に、.
大きなファイルはメモリ使用量の高いものに影響します。. たmapi_message::from_file() すべてのロード メモリにファイルを追加する. 大型ファイルの場合は、CFBコンテナを開きます。 cfb_reader すべての添付ファイルを完全に実現することなく個々のストリームにアクセスする。.
add_attachment() コンパイル時間に表示されていません。. 正しい使い方を確実にしているか確認する 含む: "aspose/email/foss/msg/mapi_message.hpp".方法は、メンバーのひとつです。 aspose::email::foss::msg::mapi_message.
よくある質問
どんなタイプの MIME を添付物に使用するべきですか?
ファイル形式のための標準IANA MIME タイプを使用します。: "text/plain" に .txt, "application/pdf" に .pdf, "application/octet-stream" 一般的なFallback for 二重ファイル. MIME タイプは、その形式として保存されます。 attach_mime_tag 資産の持ち主。.
同じファイルを複数回追加できますか?
はい. each call to add_attachment() 独立した入力が付属します.Duplicate ファイル名は許可されています; 添付リストの位置によって区別されます。.
Inline 画像の Content-ID を設定するにはどうすればよいですか?
コンテンツ ID 文字列を 4 番目の論点として転送する mapi_attachment::from_stream():
auto att = aspose::email::foss::msg::mapi_attachment::from_bytes(
"logo.png", png_bytes, "image/png", "<logo-cid@example.com>");content_id 露出したように attachment.content_id 読み返すときには、.
は attachment.data コピーか参考か?
attachment.data はA std::vector<std::uint8_t> 内側の価値で保存される mapi_message アクセスすると、追加の I/O が発生しません。.
保存前に添付ファイルを削除できますか?
ザ・ mapi_message API は、バージョン内の直接削除付属方法を露出しません。 0.1.0. Build a new message, copy the desired properties and attachments manually, 新しいオブジェクトを保存する。.