File SDK – Verarbeiten von E-Mail-.msg Dateien (C#)

Das Datei-SDK unterstützt Bezeichnungsvorgänge für .msg-Dateien genauso wie für jeden anderen Dateityp, mit der Ausnahme, dass in der Anwendung das MSG-Featureflag aktiviert sein muss. In dieser Schnellstartanleitung wird gezeigt, wie Sie dieses Kennzeichen festlegen.

Wie bereits erwähnt, erfordert die Instanziierung von IFileEngine das FileEngineSettings Konfigurationsobjekt. Die Anwendung verwendet FileEngineSettings , um benutzerdefinierte Einstellungen für eine bestimmte Instanz zu übergeben. Die CustomSettings-Eigenschaft von FileEngineSettings setzt das enable_msg_file_type-Flag, um die Verarbeitung von .msg-Dateien zu aktivieren.

Voraussetzungen

Falls Sie dies noch nicht getan haben, stellen Sie vor dem Fortfahren sicher, dass die folgenden Voraussetzungen erfüllt sind.

Das Festlegen von enable_msg_file_type und die Verwendung des File SDK zum Bezeichnen von .msg-Dateien

Ändern Sie in Fortsetzung des Schnellstarts zur Initialisierung der File-API-Anwendung den Konstruktionscode der File-Engine, um enable_msg_file_type flag festzulegen, und verwenden Sie dann die File-Engine, um eine MSG-Datei zu bezeichnen.

  1. Öffnen Sie die Visual Studio-Lösung, die Sie im vorherigen "Schnellstart: Initialisierung der Datei-SDK-Anwendung (C#)" erstellt haben.

  2. Öffnen Sie in Projektmappen-Explorer die datei .cs in Ihrem Projekt, die die Implementierung der Main() Methode enthält. Standardmäßig weist sie denselben Namen wie das Projekt auf, in dem sie enthalten ist. Diesen Namen haben Sie bei der Projekterstellung angegeben.

  3. Entfernen Sie die Implementierung der Main() Funktion aus der vorherigen Schnellstartanleitung. Fügen Sie im Textkörper Main() den folgenden Code ein. Im folgenden Codeblock wird bei der Erstellung der Datei-Engine das Flag enable_msg_file_type gesetzt. IFileHandler Mithilfe des Dateimoduls erstellte Objekte können dann eine .msg Datei verarbeiten.

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

    Weitere Informationen zu Dateivorgängen finden Sie unter "Dateihandlerkonzepte".

  4. Ersetzen Sie die Platzhalterwerte im Quellcode durch die folgenden Werte:

    Platzhalter Wert
    <Eingabedateipfad> Der vollständige Pfad zu einer Testeingabemeldungsdatei, z. B.: c:\\Test\\message.msg.
    <Ausgabedateipfad> Der vollständige Pfad zur Ausgabedatei, bei der es sich um eine beschriftete Kopie der Eingabedatei handelt, z. B.: c:\\Test\\message_labeled.msg.
    <Bezeichnungs-ID> Die Bezeichner-ID, die mithilfe der Datei-Engine abgerufen wurde, z. B.: 667466bf-a01b-4b0a-8bbf-a79a3d96f720.

Erstellen und Testen der Anwendung

Verwenden Sie F6 (Buildlösung), um Ihre Clientanwendung zu erstellen. Wenn keine Buildfehler auftreten, verwenden Sie F5 (Debuggen starten), um Ihre Anwendung auszuführen.

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