File SDK - .msg-bestanden van e-mail verwerken (C#)

De File SDK ondersteunt labelbewerkingen voor .msg bestanden op dezelfde manier als elk ander bestandstype, behalve dat de SDK de toepassing nodig heeft om de MSG-functievlag in te schakelen. In deze quickstart ziet u hoe u deze vlag instelt.

Zoals eerder besproken, vereist instantiƫren IFileEngine het FileEngineSettings instellingenobject. De toepassing gebruikt FileEngineSettings om aangepaste instellingen voor een bepaald exemplaar door te geven. De CustomSettings eigenschap van FileEngineSettings stelt de enable_msg_file_type vlag in om verwerking van .msg bestanden mogelijk te maken.

Prerequisites

Als u dat nog niet hebt gedaan, moet u de volgende vereisten voltooien voordat u doorgaat:

Enable_msg_file_type instellen en Bestands-SDK gebruiken voor het labelen van .msg bestand

Als vervolg op de quickstart voor de initialisatie van de File API-toepassing wijzigt u de code voor het maken van de bestandsengine om enable_msg_file_type flag in te stellen en gebruikt u vervolgens de bestandsengine om een .msg-bestand te labelen.

  1. Open de Visual Studio-oplossing die u in de vorige 'Quickstart: Initialisatie van bestands-SDK-toepassingen (C#)' hebt gemaakt.

  2. Open in Solution Explorer het bestand .cs in uw project dat de implementatie van de Main() methode bevat. Deze wordt standaard ingesteld op dezelfde naam als het project dat het bevat, die u hebt opgegeven tijdens het maken van het project.

  3. Verwijder de implementatie van de Main() functie uit de vorige quickstart. Voeg in de Main() hoofdtekst de volgende code in. In het volgende codeblok wordt bij het aanmaken van de bestandsengine de vlag enable_msg_file_type ingesteld. IFileHandler objecten die zijn gemaakt met behulp van de bestandsengine, kunnen vervolgens een .msg-bestand verwerken.

    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;
    }
    
    

    Zie bestandshandlerconcepten voor meer informatie over bestandsbewerkingen.

  4. Vervang de tijdelijke aanduidingen in de broncode door de volgende waarden:

    Placeholder Value
    <input-file-path> Het volledige pad naar een testinvoerberichtbestand, bijvoorbeeld: c:\\Test\\message.msg.
    <uitvoer-bestand-pad> Het volledige pad naar het uitvoerbestand, een gelabelde kopie van het invoerbestand, bijvoorbeeld: c:\\Test\\message_labeled.msg
    <label-id> De label-id die is opgehaald met behulp van de bestandsengine, bijvoorbeeld: 667466bf-a01b-4b0a-8bbf-a79a3d96f720.

De toepassing bouwen en testen

Gebruik F6 (Build Solution) om uw clienttoepassing te bouwen. Als u geen buildfouten hebt, gebruikt u F5 (Foutopsporing starten) om uw toepassing uit te voeren.

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