Sdílet prostřednictvím


Sada FILE SDK – Zpracování e-mailových souborů .msg (C++)

Sada File SDK podporuje operace označování pro soubory .msg způsobem identickým s jakýmkoli jiným typem souboru, s tím rozdílem, že sada SDK potřebuje aplikaci k povolení příznaku funkce MSG. Tady se dozvíte, jak tento příznak nastavit.

Jak bylo popsáno dříve, vytvoření instance mip::FileEngine vyžaduje objekt nastavení , mip::FileEngineSettings. FileEngine Nastavení lze použít k předání parametrů pro vlastní nastavení, které musí aplikace nastavit pro konkrétní instanci. CustomSettings vlastnost je mip::FileEngineSettings použita k nastavení příznaku pro enable_msg_file_type povolení zpracování souborů .msg.

Předpoklady

Pokud jste to ještě neudělali, nezapomeňte před pokračováním dokončit následující požadavky:

Kroky implementace předpokladů

  1. Otevřete řešení sady Visual Studio, které jste vytvořili v předchozím článku Rychlý start: Inicializace klientské aplikace (C++).

  2. Vytvořte skript PowerShellu pro generování přístupových tokenů, jak je vysvětleno v rychlém startu "Výpis popisků citlivosti (C++)".

  3. Implementujte třídu pozorovatele pro monitorování mip::FileHandler , jak je vysvětleno v rychlém startu "Set/get sensitivity labels (C++)".

Nastavení enable_msg_file_type a použití sady File SDK k označení souboru .msg

Přidejte níže uvedený kód konstrukce souborového stroje, který nastavíte enable_msg_file_type flag a použijete modul souborů k označení souboru .msg.

  1. Pomocí Průzkumník řešení otevřete soubor .cpp v projektu, který obsahuje implementaci main() metody. Ve výchozím nastavení se použije stejný název jako projekt, který obsahuje, který jste zadali při vytváření projektu.

  2. Do horní části souboru přidejte následující #include a direktivy using pod odpovídající existující direktivy:

    #include "filehandler_observer.h" 
    #include "mip/file/file_handler.h" 
    #include <iostream>    
    using mip::FileHandler;   
    using std::endl;
    
  3. Odeberte implementaci funkce z předchozího rychlého main() startu. main() Do těla vložte následující kód. V níže uvedeném příznaku bloku enable_msg_file_type kódu je nastaven během vytváření modulu souborů soubor .msg lze poté zpracovávat mip::FileHandler objekty vytvořené pomocí modulu souborů.

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;
}

Další podrobnosti o operacích se soubory najdete v konceptech obslužné rutiny souborů.

  1. Zástupné hodnoty ve zdrojovém kódu nahraďte následujícími hodnotami:

    Zástupný symbol Hodnota
    <ID aplikace> ID aplikace zaregistrované v tenantovi Microsoft Entra, například: 0edbblll-8773-44de-b87c-b8c6276d41eb.
    <účet engine-account> Účet použitý pro identitu stroje, například: user@tenant.onmicrosoft.com.
    <stav motoru> Stav uživatelem definované aplikace, například: My engine state.
    <input-file-path> Úplná cesta k testovacímu vstupnímu souboru zprávy, například: c:\\Test\\message.msg.
    <output-file-path> Úplná cesta k výstupnímu souboru, což bude označená kopie vstupního souboru, například: c:\\Test\\message_labeled.msg.
    <label-id> Id label načteného pomocí ListSensitivityLabels, například: 667466bf-a01b-4b0a-8bbf-a79a3d96f720.

Sestavení a otestování aplikace

K sestavení klientské aplikace použijte klávesu F6 (Build Solution). Pokud nemáte žádné chyby sestavení, spusťte aplikaci pomocí klávesy F5 (spustit ladění).