文件 SDK - 处理电子邮件 .msg 文件 (C++)

文件 SDK 支持以与任何其他文件类型相同的方式对 .msg 文件进行标记操作,但 SDK 需要应用程序启用 MSG 功能标志。 在这里,我们将看到如何设置此标志。

如前文所述,mip::FileEngine 的实例化需要设置对象 mip::FileEngineSettings。 FileEngineSettings 可用于传递应用程序需要为特定实例设置的自定义设置的参数。 mip::FileEngineSettingsCustomSettings 属性用于设置 enable_msg_file_type 的标志以启用 .msg 文件的处理。

先决条件

如果尚未完成,请确保先完成以下先决条件,然后再继续:

先决条件实现步骤

  1. 打开你在前文“快速入门:客户端应用程序初始化 (C++)”中创建的 Visual Studio 解决方案。

  2. 如快速入门“列出敏感度标签 (C++)”中所述,创建 PowerShell 脚本以生成访问令牌。

  3. 如快速入门“设置/获取敏感度标签 (C++)”中所述,实现观察程序类以监视 mip::FileHandler

设置 enable_msg_file_type 并使用文件 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
    <engine-account> 用于引擎标识的帐户,例如: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(启动调试)运行应用程序。