Aspose.Email.Examples.CSharp.Email.EncryptAndDecryptMessage.Run C# (CSharp) Method

Run() public static method

public static Run ( ) : void
return void
        public static void Run()
        {
            // ExStart:EncryptAndDecryptMessage
            // The path to the File directory.
            string dataDir = RunExamples.GetDataDir_Email();

            string publicCertFile = dataDir + "MartinCertificate.cer";
            string privateCertFile = dataDir + "MartinCertificate.pfx";

            X509Certificate2 publicCert = new X509Certificate2(publicCertFile);
            X509Certificate2 privateCert = new X509Certificate2(privateCertFile, "anothertestaccount");

            // Create a message
            MailMessage msg = new MailMessage("[email protected]", "[email protected]", "Test subject", "Test Body");

            // Encrypt the message
            MailMessage eMsg = msg.Encrypt(publicCert);
            if (eMsg.IsEncrypted == true)
                Console.WriteLine("Its encrypted");
            else
                Console.WriteLine("Its NOT encrypted");

            // Decrypt the message
            MailMessage dMsg = eMsg.Decrypt(privateCert);
            if (dMsg.IsEncrypted == true)
                Console.WriteLine("Its encrypted");
            else
                Console.WriteLine("Its NOT encrypted");
            // ExEnd:EncryptAndDecryptMessage
        }
    }
EncryptAndDecryptMessage