Sample code for 30+ languages & platforms
SQL Server

S/MIME Encrypt .eml without Sending

See more Email Object Examples

Demonstrates how to encrypt an email using the recipient's digital certificate. This example just encrypts, and does not send the email.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    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_OAMethod @email, 'LoadEml', @success OUT, 'c:/temp/email/unencrypted.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    -- The email content is encrypted using AES with a 256-bit key, operating in GCM mode, which provides authenticated encryption.
    EXEC sp_OASetProperty @email, 'Pkcs7CryptAlg', 'aes-gcm'
    EXEC sp_OASetProperty @email, 'Pkcs7KeyLength', 256
    EXEC sp_OASetProperty @email, 'OaepPadding', 1
    EXEC sp_OASetProperty @email, 'OaepHash', 'sha256'
    EXEC sp_OASetProperty @email, 'OaepMgfHash', 'sha256'

    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, 'c/temps/cert/recipient.cer'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    EXEC sp_OASetProperty @email, 'SendEncrypted', 1
    EXEC sp_OAMethod @email, 'SetEncryptCert', @success OUT, @cert

    DECLARE @sbSmime int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbSmime OUT

    -- The mailman object applies the encryption by rendering the email according to the instructions (property settings) provided in the email object.
    -- No email is sent.
    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mailman OUT

    EXEC sp_OAMethod @mailman, 'RenderToMimeSb', @success OUT, @email, @sbSmime
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @sbSmime
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END

    EXEC sp_OAMethod @sbSmime, 'WriteFile', @success OUT, 'c:/temp/encryptedEmail.eml', 'utf-8', 0
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @sbSmime
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END


    PRINT 'Success!'

    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @sbSmime
    EXEC @hr = sp_OADestroy @mailman


END
GO