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

檔案 SDK 支援以與任何其他檔案類型相同的方式為 .msg 檔案套用標籤作業,但 SDK 需要應用程式才能啟用 MSG 功能旗標。 在這裡,我們將瞭解如何設定此旗標。

如先前所述,的 IFileEngine 具現化需要設定物件 FileEngineSettings 。 FileEngine設定可用來傳遞應用程式針對特定實例設定所需的自訂設定參數。 CustomSettingsFileEngineSettings 屬性是用來設定 旗 enable_msg_file_type 標,以啟用 .msg 檔案的處理。

必要條件

如果您尚未完成,請務必先完成下列必要條件,再繼續進行:

設定enable_msg_file_type並使用檔案 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);
    
        MipContext mipContext = MIP.CreateMipContext(appInfo,"mip_data",LogLevel.Trace,null,null);
    
        // 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 = options.AssignmentMethod
        };
    
        fileHandler.SetLabel(labelId, 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.Name));
        Console.WriteLine("Press a key to continue.");
        Console.ReadKey();
    
        // Application Shutdown
        fileHandler = null;
        handlerModified = null;
        fileEngine = null;
        fileProfile = null;
        mipContext = null;
    }
    
    

    如需檔案作業的進一步詳細資料, 請參閱檔案處理常式概念

  4. 使用下列值取代原始程式碼中的預留位置值:

    預留位置
    <input-file-path> 測試輸入訊息檔的完整路徑,例如: c:\\Test\\message.msg
    <output-file-path> 輸出檔案的完整路徑,這會是輸入檔的標籤複本,例如: c:\\Test\\message_labeled.msg
    <label-id> 使用檔案引擎擷取的 labelId,例如: 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.