Dosya SDK'sı - E-posta .msg dosyalarını işleme (C++)

Dosya SDK'sı, .msg dosyaları için etiketleme işlemlerini diğer dosya türleriyle aynı şekilde destekler, ancak SDK'nın MSG özellik bayrağını etkinleştirmek için uygulamaya ihtiyacı vardır. Bu hızlı başlangıçta bu bayrağın nasıl ayarlanacağı gösterilmektedir.

Daha önce açıklandığı gibi, mip::FileEngine örneğini oluşturmak için mip::FileEngineSettings ayar nesnesi gerekir. Uygulama, belirli bir örnek için özel ayarları iletmek üzere FileEngineSettings kullanır. CustomSettings öğesinin mip::FileEngineSettings özelliği, .msg dosyalarının işlenmesini etkinleştirmek için enable_msg_file_type bayrağını ayarlar.

Önkoşullar

Henüz yapmadıysanız devam etmeden önce aşağıdaki önkoşulları tamamladığınızdan emin olun:

Önkoşul uygulama adımları

  1. Önceki "Hızlı Başlangıç: İstemci uygulaması başlatma (C++)" makalesinde oluşturduğunuz Visual Studio çözümünü açın.

  2. Hızlı Başlangıç: Duyarlılık etiketlerini listeleme (C++) bölümünde açıklandığı gibi erişim belirteçleri oluşturmak için bir PowerShell betiği oluşturun.

  3. Hızlı Başlangıç: Duyarlılık etiketlerini ayarlama/alma (C++) bölümünde açıklandığı gibi izlemek mip::FileHandleriçin bir gözlemci sınıfı uygulayın.

.msg dosyasını etiketlemek için enable_msg_file_type ayarlayın ve Dosya SDK'sını kullanın

bayrağını ayarlamak enable_msg_file_type için aşağıdaki dosya altyapısı yapı kodunu ekleyin ve dosya altyapısını kullanarak bir .msg dosyasını etiketleyin.

  1. Çözüm Gezgini'da, yönteminin uygulanmasını main() içeren .cpp dosyasını projenizde açın. Proje oluşturma sırasında belirlediğiniz isim, varsayılan olarak projeyle aynı olup onu içeren yapıdadır.

  2. Aşağıdaki #include ve kullanma yönergelerini ilgili mevcut yönergelerin arkasına dosyanın en üstüne ekleyin:

    #include "filehandler_observer.h" 
    #include "mip/file/file_handler.h" 
    #include <iostream>    
    using mip::FileHandler;   
    using std::endl;
    
  3. Önceki hızlı başlangıçtan main() işlevinin uygulamasını kaldırın. main() gövdesine aşağıdaki kodu ekleyin. Aşağıdaki kod bloğunda, dosya altyapısının oluşturulması enable_msg_file_type bayrağını ayarlar. mip::FileHandler dosya altyapısı kullanılarak oluşturulan nesneler daha sonra bir .msg dosyasını işleyebilir.

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>(appInfo,
				                                                                                       "mip_data",
                                                                                        			   mip::LogLevel::Trace,
                                                                                                       false,
                                                                                                       mip::CacheStorageType::OnDisk);

    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(mMipContext, mip::CacheStorageType::OnDisk,
        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)
                            authDelegateImpl,                       // Token acquisition implementation
                            "<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
    auto labels = engine->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);

    // Resolve the label ID to a mip::Label, then apply it.
    auto label = engine->GetLabelById(labelId);
    fileHandler->SetLabel(label, 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 = msgHandlerPromise->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->GetLabel()->GetLabel()->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;
    mMipContext->ShutDown();
    mMipContext = nullptr;

    return 0;
}

Dosya işlemleri hakkında daha fazla bilgi için bkz. Dosya İşleyicisi kavramları.

  1. Kaynak koddaki yer tutucu değerlerini aşağıdaki değerlerle değiştirin:

    Yer tutucu Değer
    <uygulama kimliği> Microsoft Entra kiracısına kayıtlı uygulama kimliği, örneğin: 00001111-aaaa-2222-bbbb-3333cccc4444.
    <altyapı hesabı> Motor kimliği için kullanılan hesap, örneğin: user@tenant.onmicrosoft.com.
    <altyapı durumu> Kullanıcı tanımlı uygulama durumu, örneğin: My engine state.
    <girdi dosya yolu> Bir test girdi mesaj dosyasının tam yolu, örneğin: c:\\Test\\message.msg.
    <çıkış dosyası yolu> Çıkış dosyasının tam yolu; giriş dosyasının etiketli bir kopyasıdır, örneğin: c:\\Test\\message_labeled.msg.
    <label-id> ListSensitivityLabels kullanılarak alınan etiket kimliği, örneğin: 667466bf-a01b-4b0a-8bbf-a79a3d96f720.

Uygulamayı derleme ve test etme

F6'yı (Çözümü Derle) kullanarak istemci uygulamanızı derleyin. Derleme hataları yoksa, uygulamanızı çalıştırmak için F5 (Hata ayıklamayı başlat) kullanın.