次の方法で共有


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

File SDK では、他のファイルの種類と同じ方法で.msg ファイルのラベル付け操作がサポートされます。ただし、SDK では、アプリケーションで MSG 機能フラグを有効にする必要があります。 ここでは、このフラグを設定する方法について説明します。

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

[前提条件]

まだ行っていない場合は、続行する前に次の前提条件を満たしていることを確認してください。

前提条件の実装手順

  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. 次の値を使用して、ソース コード内のプレースホルダーの値を置き換えます。

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

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

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