Sample code for 30+ languages & platforms
SQL Server

Password-Encrypt an Email with AES

See more Email Object Examples

Demonstrates the Chilkat Email.AesEncrypt method, which encrypts the email body, all alternative bodies, all message sub-parts, and attachments using 128-bit AES CBC encryption. This is symmetric, password-based encryption — it does not use certificates or private keys. To decrypt, call AesDecrypt with the same password. Both the sending and receiving applications must use Chilkat and must know the shared password. This example encrypts an email.

Background: This is a Chilkat-specific convenience, distinct from standard S/MIME. Symmetric encryption means one shared secret (the password) both locks and unlocks the message — simple, but it requires a secure way to share that password out-of-band, and only Chilkat-based software can read the result. S/MIME (SendEncrypted), by contrast, uses public-key certificates and interoperates with standard mail clients. Choose AES password encryption for Chilkat-to-Chilkat exchanges where distributing a shared password is practical.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    --  Demonstrates the AesEncrypt method, which encrypts the email body, all alternative
    --  bodies, all sub-parts, and attachments using 128-bit AES CBC encryption with a password.
    --  This is symmetric, password-based encryption -- it does not use certificates.  Both the
    --  sending and receiving applications must use Chilkat and know the same password.

    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @email, 'Subject', 'Confidential'
    EXEC sp_OASetProperty @email, 'Body', 'This body will be AES-encrypted with a password.'

    --  Encrypt the entire email using a shared password.
    DECLARE @success int
    EXEC sp_OAMethod @email, 'AesEncrypt', @success OUT, 'secret_password'

    --  The email content is now encrypted.  Call AesDecrypt with the same password to restore it.
    EXEC sp_OAMethod @email, 'GetMime', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @email


END
GO