C++ にて VMime を使って 添付ファイル付きのメールを送信する

C++ にて VMime を使って メールを送信する
の続きです。

VMime の添付ファイル

mime::fileAttachment を使って、添付ファイルを作成する。
vmime::messageBuilder に追加する。

下記は、VMime Book に掲載されている例

VMime Book 添付ファイルの追加

// 添付ファイルを作成する
vmime: : shared ptr <vmime::fileAttachment> att = vmime::make shared <vmime::fileAttachment>
(
/∗ full path to file ∗/ ”/home/vincent/paris.jpg”,
/∗ content type ∗/ vmime::mediaType(”image/jpeg),
/∗ description ∗/ vmime::text(”My holidays in Paris”)
);

// ファイルに関する情報を設定することもできる
att−>getFileInfo().setFilename(”paris.jpg”); att−>getFileInfo().setCreationDate
(vmime::datetime(”30 Apr 2003 14:30:00 +0200”));

// この添付ファイルをメッセージに追加する
mb.appendAttachment(att);

同封されているサンプルコード: example2.cpp
https://github.com/kisli/vmime/blob/master/examples/example2.cpp

添付ファイルを絶対パスで指定する

example2.cpp を実行すると、エラーになる。

vmime::exception: Error opening file

相対パスで指定すると、うまくいかず。 絶対パスで指定すると、よい。

ソースコードにに絶対パスをハードコーディングすると、移植性が悪い。
実行中のプログラムのファイルパスを取得し、絶対パスにする。

参考 : C言語にて 実行中のプログラムのファイルパスを取得する

全体のコードは、githubに公開した。
https://github.com/ohwada/MAC_cpp_Samples/tree/master/vmime/smtp