檔案 SDK 支援.msg檔案的標籤操作,與其他檔案類型相同,但 SDK 需要應用程式啟用 MSG 功能標誌。 這個快速入門說明如何設定這個旗幟。
如前所述,將 mip::FileEngine 具現化需要 mip::FileEngineSettings 設定物件。 應用程式會用 FileEngineSettings 來傳遞特定實例的自訂設定。
mip::FileEngineSettings 的 CustomSettings 屬性會設定 enable_msg_file_type 旗標,以啟用 .msg 檔案的處理。
先決條件
如果您尚未完成,請務必先完成下列必要條件,再繼續進行:
- 首先完成 快速入門:檔案 SDK 應用程式初始化 (C++),這將建立初始化的 Visual Studio 專案解決方案。 本「如何 - 處理電子郵件訊息 .msg 檔案 (C++)」快速入門是以先前的快速入門為基礎。
- 檢閱快速入門:列出敏感度標籤(C++)。
- 檢閱快速入門:設定/取得敏感度標籤(C++)。
- 檢閱 電子郵件檔案 MIP SDK 概念。
- 選擇性:檢閱 MIP SDK 概念中的檔案引擎 。
- 選擇性:檢閱 MIP SDK 概念中的檔案處理程式 。
必要條件實作步驟
開啟您在上一篇「快速入門:用戶端應用程式初始化(C++)》一文中建立的 Visual Studio 解決方案。
建立一個 PowerShell 腳本來產生存取權杖,詳見 快速入門:List sensitivity labels (C++) 中說明。
實作一個觀察器類別來監控
mip::FileHandler,如快速入門說明:設定/取得敏感性標籤(C++)。
設定 enable_msg_file_type 並使用檔案 SDK 來標記 .msg 檔案
加入以下檔案引擎建構程式碼來設定 enable_msg_file_type 旗標,並用檔案引擎標記.msg檔案。
在 方案總管 中,打開專案中包含該方法實作
main()的 .cpp 檔案。 它預設為與包含它的專案相同的名稱,此專案名稱是在您建立專案時指定的。在檔案頂端的對應現有指示詞後方,加入下列 #include 和 using 指示詞:
#include "filehandler_observer.h" #include "mip/file/file_handler.h" #include <iostream> using mip::FileHandler; using std::endl;移除上一個快速入門中的
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>(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;
}
欲了解更多關於檔案操作的資訊,請參閱 檔案處理程序概念。
將原始碼中的佔位值替換為以下值:
佔位符 價值 <應用程式識別碼> 以 Microsoft Entra 租戶註冊的應用程式 ID 為例: 00001111-aaaa-2222-bbbb-3333cccc4444。<引擎帳戶> 例如,用於引擎身份的帳號為: user@tenant.onmicrosoft.com。<引擎狀態> 使用者自訂的應用程式狀態,例如: My engine state。<輸入檔案路徑> 測試輸入訊息檔的完整路徑,例如: c:\\Test\\message.msg。<輸出檔案路徑> 輸出檔案的完整路徑,該路徑是輸入檔案的標記副本,例如: c:\\Test\\message_labeled.msg。<標籤識別碼> 使用 ListSensitivityLabels擷取的標籤 ID,例如:667466bf-a01b-4b0a-8bbf-a79a3d96f720。
建置及測試應用程式
使用 F6 (建置解決方案) 來建置用戶端應用程式。 如果沒有建置錯誤,就用 F5 (開始除錯)來執行你的應用程式。