File SDK - メールの .msg ファイルを処理する (C++)

File SDK は、アプリケーションで MSG 機能フラグを有効にしなければならない点を除き、他の種類のファイルとまったく同じ方法で .msg ファイルのラベル付け操作をサポートします。 ここでは、このフラグを設定する方法を見ていきます。

既に説明したように、mip::FileEngine をインスタンス化するためには、設定オブジェクト mip::FileEngineSettings が必要となります。 特定のインスタンスに対してアプリケーションが設定する必要のあるカスタム設定のパラメーターは、FileEngineSettings を使用して渡すことができます。 .msg ファイルの処理を有効にする enable_msg_file_type のフラグは、mip::FileEngineSettingsCustomSettings プロパティを使用して設定します。

前提条件

先に進む前に、次の前提条件をまだ実行していない場合は完了してください。

前提条件の実装手順

  1. 先行する記事「クイックスタート: クライアント アプリケーションの初期化 (C++)」で作成した Visual Studio ソリューションを開きます。

  2. 秘密度ラベルの一覧表示 (C++)」のクイックスタートの説明に従って、アクセス トークンを生成するための PowerShell スクリプトを作成します。

  3. 秘密度ラベルの設定と取得 (C++)」のクイックスタートの説明に従って、mip::FileHandler を監視するためのオブザーバー クラスを実装します。

enable_msg_file_type を設定し File SDK を使用して .msg ファイルのラベル付けを行う

以下のファイル エンジンの構築コードを追加して enable_msg_file_type flag を設定し、ファイル エンジンを使用して .msg ファイルのラベル付けを行います。

  1. ソリューション エクスプローラーを使用して、main() メソッドの実装を含むプロジェクトの .cpp ファイルを開きます。 既定の名前は、それが含まれるプロジェクトと同じであり、プロジェクトの作成時に指定したものです。

  2. ファイルの先頭に、次の #include および using ディレクティブを対応する既存のディレクティブの下に追加します。

    #include "filehandler_observer.h" 
    #include "mip/file/file_handler.h" 
    #include <iostream>    
    using mip::FileHandler;   
    using std::endl;
    
  3. 以前のクイックスタートから main() 関数の実装を削除します。 main() の本体内に、次のコードを挿入します。 下のコード ブロックでは、ファイル エンジンの作成中に enable_msg_file_type フラグが設定されています。その後、そのファイル エンジンを使用して作成された mip::FileHandler オブジェクトで .msg ファイルを処理することができます。

int main()
{
    // Construct/initialize objects required by the application's profile object
    ApplicationInfo appInfo { "<application-id>",                    // ApplicationInfo object (App ID, name, version)
                              "<application-name>", 
                              "1.0" 
    };

    std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
				                                                                                       "mip_data",
                                                                                        			   mip::LogLevel::Trace,
                                                                                                       false);

    std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);

    auto profileObserver = make_shared<ProfileObserver>();                      // Observer object
    auto authDelegateImpl = make_shared<AuthDelegateImpl>("<application-id>");  // Authentication delegate object (App ID)
    auto consentDelegateImpl = make_shared<ConsentDelegateImpl>();              // Consent delegate object

    // Construct/initialize profile object
    FileProfile::Settings profileSettings(mipContext,mip::CacheStorageType::OnDisk,authDelegateImpl,
        consentDelegateImpl,profileObserver);

    // Set up promise/future connection for async profile operations; load profile asynchronously
    auto profilePromise = make_shared<promise<shared_ptr<FileProfile>>>();
    auto profileFuture = profilePromise->get_future();
    try
    {
        mip::FileProfile::LoadAsync(profileSettings, profilePromise);
    }
    catch (const std::exception& e)
    {
        std::cout << "An exception occurred. Are the Settings and ApplicationInfo objects populated correctly?\n\n"<< e.what() << "'\n";
        system("pause");
        return 1;
    }

    auto profile = profileFuture.get();

    // Construct/initialize engine object
    FileEngine::Settings engineSettings(
                            mip::Identity("<engine-account>"),      // Engine identity (account used for authentication)
                            "<engine-state>",                       // User-defined engine state
                            "en-US");                               // Locale (default = en-US)

    //Set enable_msg_file_type flag as true
    std::vector<std::pair<string, string>> customSettings;
    customSettings.emplace_back(mip::GetCustomSettingEnableMsgFileType(), "true");
    engineSettings.SetCustomSettings(customSettings);

    // Set up promise/future connection for async engine operations; add engine to profile asynchronously
    auto enginePromise = make_shared<promise<shared_ptr<FileEngine>>>();
    auto engineFuture = enginePromise->get_future();
    profile->AddEngineAsync(engineSettings, enginePromise);
    std::shared_ptr<FileEngine> engine;

    try
    {
        engine = engineFuture.get();
    }
    catch (const std::exception& e)
    {
        cout << "An exception occurred... is the access token incorrect/expired?\n\n"<< e.what() << "'\n";
        system("pause");
        return 1;
    }

    //Set file paths
    string inputFilePath = "<input-file-path>"; //.msg file to be labeled
    string actualFilePath = inputFilePath;
    string outputFilePath = "<output-file-path>"; //labeled .msg file
    string actualOutputFilePath = outputFilePath;

    //Create a file handler for original file
    auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<FileHandler>>>();
    auto handlerFuture = handlerPromise->get_future();

    engine->CreateFileHandlerAsync(inputFilePath,
                                    actualFilePath,
                                    true,
                                    std::make_shared<FileHandlerObserver>(),
                                    handlerPromise);

    auto fileHandler = handlerFuture.get();

    //List labels available to the user    

    // Use mip::FileEngine to list all labels
    labels = mEngine->ListSensitivityLabels();

    // Iterate through each label, first listing details
    for (const auto& label : labels) {
        cout << label->GetName() << " : " << label->GetId() << endl;

        // get all children for mip::Label and list details
        for (const auto& child : label->GetChildren()) {
            cout << "->  " << child->GetName() << " : " << child->GetId() << endl;
        }
    }

    string labelId = "<labelId-id>"; //set a label ID to use

    // Labeling requires a mip::LabelingOptions object. 
    // Review API ref for more details. The sample implies that the file was labeled manually by a user.
    mip::LabelingOptions labelingOptions(mip::AssignmentMethod::PRIVILEGED);

    fileHandler->SetLabel(labelId, labelingOptions, mip::ProtectionSettings());

    // Commit changes, save as outputFilePath
    auto commitPromise = std::make_shared<std::promise<bool>>();
    auto commitFuture = commitPromise->get_future();

    if(fileHandler->IsModified())
    {
        fileHandler->CommitAsync(outputFilePath, commitPromise);
    }
    
    if (commitFuture.get()) {
        cout << "\n Label applied to file: " << outputFilePath << endl;
    }
    else {
        cout << "Failed to label: " + outputFilePath << endl;
        return 1;
    }

    // Create a new handler to read the label
    auto msgHandlerPromise = std::make_shared<std::promise<std::shared_ptr<FileHandler>>>();
    auto msgHandlerFuture = handlerPromise->get_future();

    engine->CreateFileHandlerAsync(inputFilePath,
                                    actualFilePath,
                                    true,
                                    std::make_shared<FileHandlerObserver>(),
                                    msgHandlerPromise);

    auto msgFileHandler = msgHandlerFuture.get();

    cout << "Original file: " << inputFilePath << endl;
    cout << "Labeled file: " << outputFilePath << endl;
    cout << "Label applied to file : " 
            << msgFileHandler->GetName() 
            << endl;
    
    // Application shutdown. Null out profile, engine, handler.
    // Application may crash at shutdown if resources aren't properly released.
    msgFileHandler = nullptr;
    fileHandler = nullptr;
    engine = nullptr;
    profile = nullptr;
    mipContext = nullptr;

    return 0;
}

ファイル操作の詳細については、ファイル ハンドラーの概念に関する記事を参照してください。

  1. 次の値を使用して、ソース コードのプレースホルダー値を置き換えます。

    プレースホルダー Value
    <application-id> Microsoft Entra テナントに登録されたアプリケーション ID (例: 0edbblll-8773-44de-b87c-b8c6276d41eb)。
    <engine-account> エンジンの ID に使用されるアカウント (例: user@tenant.onmicrosoft.com)。
    <engine-state> ユーザー定義のアプリケーション状態 (例: My engine state)。
    <input-file-path> テスト入力メッセージ ファイルの完全なパス (例: c:\\Test\\message.msg)。
    <output-file-path> 出力ファイル、つまり入力ファイルのラベル付きコピーの完全なパス (例: c:\\Test\\message_labeled.msg)。
    <label-id> ListSensitivityLabels を使用して取得されたラベル ID (例: 667466bf-a01b-4b0a-8bbf-a79a3d96f720)。

アプリケーションのビルドとテスト

F6 キー ([ソリューションのビルド]) を使用して、クライアント アプリケーションをビルドします。 ビルド エラーがない場合は、F5 キー ([デバッグの開始]) を使用してアプリケーションを実行します。