クイックスタート

クイックスタート

クイックスタート: C++で作る最初のMSGファイル

このガイドでは、標準C++ライブラリと Aspose.Email FOSS for C++ のみを使用して、メールを作成し、MSGファイルとして保存し、再度読み込み、EMLに変換する完全な実行可能サンプルを作成します。


前提条件

要件詳細
CompilerC++17以降 (GCC 9以上、Clang 10以上、MSVC 2019以上)
ビルドシステムCMake 3.26以降
Libraryクローン元 GitHub (参照 Installation)
DependenciesNone

ステップ 1: CMakeLists.txt を設定する

cmake_minimum_required(VERSION 3.26)
project(email_quickstart)

add_subdirectory(Aspose.Email-FOSS-for-Cpp)

add_executable(quickstart main.cpp)
target_link_libraries(quickstart PRIVATE AsposeEmailFoss::AsposeEmailFoss)
set_target_properties(quickstart PROPERTIES CXX_STANDARD 17)

ステップ 2: MSGファイルを作成して保存する

件名、本文、送信者、受信者、添付ファイルを含む新しいMSGファイルを作成します:

#include <fstream>
#include <vector>
#include "aspose/email/foss/msg/mapi_message.hpp"

int main()
{
    // Compose the message
    auto message = aspose::email::foss::msg::mapi_message::create(
        "Hello from Aspose.Email FOSS",
        "This message was created programmatically — no Outlook required.");

    message.set_sender_name("Alice");
    message.set_sender_email_address("alice@example.com");
    message.add_recipient("bob@example.com", "Bob");
    message.add_attachment(
        "readme.txt",
        std::vector<std::uint8_t>{'H', 'e', 'l', 'l', 'o', '\n'},
        "text/plain");

    // Save to MSG file
    std::ofstream out("hello.msg", std::ios::binary);
    message.save(out);

    return 0;
}

ビルドして実行:

cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/quickstart

ファイル hello.msg が作業ディレクトリに作成されます。Outlook で開いて確認するか、ステップ 3 に進んでプログラムで再度読み取ります。


ステップ 3: MSG ファイルを読み戻す

作成したファイルを読み込み、プロパティを表示します:

#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"

int main()
{
    std::ifstream input("hello.msg", std::ios::binary);
    const auto message = aspose::email::foss::msg::mapi_message::from_stream(input);

    std::cout << "Subject:     " << message.subject() << '\n';
    std::cout << "From:        " << message.sender_name()
              << " <" << message.sender_email_address() << ">\n";
    std::cout << "Body:        " << message.body() << '\n';
    std::cout << "Recipients:  " << message.recipients().size() << '\n';
    std::cout << "Attachments: " << message.attachments().size() << '\n';

    for (const auto& a : message.attachments())
        std::cout << "  - " << a.filename << " (" << a.data.size() << " bytes)\n";

    return 0;
}

期待される出力:

Subject:     Hello from Aspose.Email FOSS
From:        Alice <alice@example.com>
Body:        This message was created programmatically — no Outlook required.
Recipients:  1
Attachments: 1
  - readme.txt (6 bytes)

ステップ 4: EML に変換

MSG ファイルを save_to_eml() を使用して RFC 5322 / MIME 形式に変換します:

#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("hello.msg"));

    message.save_to_eml(std::filesystem::path("hello.eml"));
    std::cout << "Saved hello.eml\n";

    return 0;
}

件名、本文、送信者、受信者、そして添付ファイルはすべて EML 出力に保持されます。


次のステップ

  • Installation — 完全な CMake プロジェクト設定とサポート対象プラットフォーム
  • Features — C++ 例を含む完全な機能リファレンス
  • 開発者ガイド — MSG、EML、CFB、および MAPI プロパティのウォークスルー
  • FAQ — ライセンス、フォーマットサポート、既知の制限

参照

 日本語