文件 SDK - 处理电子邮件 .msg 文件 (C#)

文件 SDK 支持以与任何其他文件类型相同的方式对 .msg 文件进行标记操作,但 SDK 需要应用程序启用 MSG 功能标志。 在这里,我们将看到如何设置此标志。

如前文所述,IFileEngine 的实例化需要设置对象 FileEngineSettings。 FileEngineSettings 可用于传递应用程序需要为特定实例设置的自定义设置的参数。 FileEngineSettingsCustomSettings 属性用于设置 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> 使用文件引擎检索到的标签 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.