File SDK - 處理電子郵件 .msg 檔案(C#)

檔案 SDK 支援.msg檔案的標籤操作,與其他檔案類型相同,但 SDK 需要應用程式啟用 MSG 功能標誌。 這個快速入門說明如何設定這個旗幟。

如前所述,將 IFileEngine 具現化需要 FileEngineSettings 設定物件。 應用程式會用 FileEngineSettings 來傳遞特定實例的自訂設定。 FileEngineSettings 的 CustomSettings 屬性會設定 enable_msg_file_type 旗標,以啟用 .msg 檔案的處理。

Prerequisites

如果你還沒完成,請務必在繼續前完成以下先決條件:

設定enable_msg_file_type並使用 File SDK 來標記.msg檔案

在檔案 API 應用程式初始化快速啟動的延續中,修改檔案引擎建構程式碼,使其設定 enable_msg_file_type flag 並使用檔案引擎來標記.msg檔案。

  1. 打開你在之前「快速入門:檔案 SDK 應用程式初始化(C#)」中建立的 Visual Studio 解決方案。

  2. 在方案總管中,打開專案中包含該方法實作Main()的 .cs 檔案。 它預設為與包含它的專案相同的名稱,此專案名稱是在您建立專案時指定的。

  3. 移除上一個快速入門中的 Main() 函式實作。 在主體內 Main() ,插入下列程序代碼。 在接下來的程式碼區塊中,建立檔案引擎時會設定 enable_msg_file_type 旗標。 IFileHandler 使用檔案引擎建立的物件接著可以處理.msg檔案。

    static void Main(string[] args)
    {
        // Initialize Wrapper for File SDK operations.
        MIP.Initialize(MipComponent.File);
    
        // Create ApplicationInfo, setting the clientID from Azure AD App Registration as the ApplicationId.
        ApplicationInfo appInfo = new ApplicationInfo()
        {
                ApplicationId = clientId,
                ApplicationName = appName,
                ApplicationVersion = "1.0.0"
        };
    
        // Instantiate the AuthDelegateImpl object, passing in AppInfo.
        AuthDelegateImplementation authDelegate = new AuthDelegateImplementation(appInfo);
    
        MipConfiguration mipConfiguration = new MipConfiguration(appInfo, "mip_data", LogLevel.Trace, false, CacheStorageType.OnDiskEncrypted);
        MipContext mipContext = MIP.CreateMipContext(mipConfiguration);
    
        // Initialize and instantiate the File Profile.
        // Create the FileProfileSettings object.
        // Initialize file profile settings to create/use local state.
        var profileSettings = new FileProfileSettings(mipContext, 
                                    CacheStorageType.OnDiskEncrypted, 
                                    new ConsentDelegateImplementation());
    
        // Load the Profile async and wait for the result.
        var fileProfile = Task.Run(async () => await MIP.LoadFileProfileAsync(profileSettings)).Result;
    
        // Create a FileEngineSettings object, then use that to add an engine to the profile.
        var customSettings = new List<KeyValuePair<string, string>>();
        customSettings.Add(new KeyValuePair<string, string>("enable_msg_file_type", "true"));
    
        // Create a FileEngineSettings object, then use that to add an engine to the profile.
        var engineSettings = new FileEngineSettings("user1@tenant.com", authDelegate, "", "en-US");
        engineSettings.Identity = new Identity("user1@tenant.com");
        //set custom settings for the engine
        engineSettings.CustomSettings = customSettings;
    
        //Add fileEngine to profile
        var fileEngine = Task.Run(async () => await fileProfile.AddEngineAsync(engineSettings)).Result;
    
        //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
        var fileHandler = Task.Run(async () => await fileEngine.CreateFileHandlerAsync(inputFilePath, 
                                                                    actualFilePath, 
                                                                    true)).Result;
    
        // List labels available to the user and use one of them to label the MSG file.
    
        foreach (var label in fileEngine.SensitivityLabels)
        {
            Console.WriteLine(string.Format("{0} - {1}", label.Name, label.Id));
    
            if (label.Children.Count > 0)
            {
                foreach (Label child in label.Children)
                {
                    Console.WriteLine(string.Format("\t{0} - {1}", child.Name, child.Id));
                }
            }
        }
    
        string labelId = "<label-id>"; //label retrieved using file engine
    
        LabelingOptions labelingOptions = new LabelingOptions()
        {
            AssignmentMethod = AssignmentMethod.Standard
        };
    
        Label label = fileEngine.GetLabelById(labelId);
        fileHandler.SetLabel(label, labelingOptions, new ProtectionSettings());
    
        // Commit changes, save as outputFilePath
        var result = Task.Run(async () => await fileHandler.CommitAsync(outputFilePath)).Result;
    
        // Create a new handler to read the labeled file metadata
        var handlerModified = Task.Run(async () => await fileEngine.CreateFileHandlerAsync(outputFilePath, 
                                                                        actualOutputFilePath, 
                                                                        true)).Result;
    
        Console.WriteLine(string.Format("Original file: {0}", inputFilePath));
        Console.WriteLine(string.Format("Labeled file: {0}", outputFilePath));
        Console.WriteLine(string.Format("Label applied to file: {0}", 
            handlerModified.Label.Label.Name));
        Console.WriteLine("Press a key to continue.");
        Console.ReadKey();
    
        // Application Shutdown
        fileHandler = null;
        handlerModified = null;
        fileEngine = null;
        fileProfile = null;
        mipContext = null;
    }
    
    

    欲了解更多關於檔案操作的資訊,請參閱 檔案處理程序概念。

  4. 將原始碼中的佔位值替換為以下值:

    Placeholder Value
    <輸入檔案路徑> 測試輸入訊息檔的完整路徑,例如: c:\\Test\\message.msg。
    <輸出檔案路徑> 輸出檔案的完整路徑,該路徑是輸入檔案的標記副本,例如: c:\\Test\\message_labeled.msg。
    <標籤識別碼> 使用檔案引擎取得的標籤 ID,例如: 667466bf-a01b-4b0a-8bbf-a79a3d96f720。

組建和測試應用程式

使用 F6 (建置解決方案) 來建置用戶端應用程式。 如果沒有建置錯誤,就用 F5 (開始除錯)來執行你的應用程式。

    Original file: C:\Test.msg
    Labeled file: C:\Test_Labeled.msg
    Label applied to file: Confidential    
    Press a key to continue.