Sample code for 30+ languages & platforms
SQL Server

Send Encrypted Email to Multiple Recipients

Demonstrates how to create and send an S/MIME encrypted email to multiple recipients. The digital certificate of each recipient is required. The encrypting/sending process uses each recipient's digital certificate (which internally contains the public key). Each recipient decrypts the received email using his/her private key.

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.

    -- The mailman object is used for sending and receiving email.
    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mailman OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Set the SMTP server.
    EXEC sp_OASetProperty @mailman, 'SmtpHost', 'smtp.mymailserver.com'

    -- Load each recipient's certificate into a Chilkat certificate object.
    -- This example loads the certificates from files.  However, the Chilkat
    -- certificate object provides other means for loading certificates,
    -- such as from in-memory PEM strings, or in-memory binary DER encoded form, etc.
    DECLARE @cert1 int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert1 OUT

    EXEC sp_OAMethod @cert1, 'LoadFromFile', @success OUT, 'recipient1.cer'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @cert1, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @cert1
        RETURN
      END
    DECLARE @cert2 int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert2 OUT

    EXEC sp_OAMethod @cert2, 'LoadFromFile', @success OUT, 'recipient2.cer'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @cert2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @cert1
        EXEC @hr = sp_OADestroy @cert2
        RETURN
      END
    DECLARE @cert3 int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert3 OUT

    EXEC sp_OAMethod @cert3, 'LoadFromFile', @success OUT, 'recipient3.cer'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @cert3, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @cert1
        EXEC @hr = sp_OADestroy @cert2
        EXEC @hr = sp_OADestroy @cert3
        RETURN
      END

    -- Create a new email object
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    EXEC sp_OASetProperty @email, 'Subject', 'This email is encrypted and sent to 3 recipients'
    EXEC sp_OASetProperty @email, 'Body', 'This is an S/MIME encrypted mail sent to 3 recipients'
    EXEC sp_OASetProperty @email, 'From', 'Chilkat Support <support@chilkatsoft.com>'

    -- Make each of the certificates available for encrypting the email
    -- by calling AddEncryptCert for each.
    EXEC sp_OAMethod @email, 'AddEncryptCert', @success OUT, @cert1
    IF @success = 1
      BEGIN
        EXEC sp_OAMethod @email, 'AddEncryptCert', @success OUT, @cert2
      END
    IF @success = 1
      BEGIN
        EXEC sp_OAMethod @email, 'AddEncryptCert', @success OUT, @cert3
      END
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @cert1
        EXEC @hr = sp_OADestroy @cert2
        EXEC @hr = sp_OADestroy @cert3
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    -- Add 3 recipients to the email (2 TO addresses, and 1 CC address)
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Recipient 1', 'admin@cknotes.com'
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Recipient 2', 'somebody001122@yahoo.com'
    EXEC sp_OAMethod @email, 'AddCC', @success OUT, 'Recipient 3', 'somebody123xyz@gmail.com'

    -- Indicate that the email is to be sent encrypted.
    EXEC sp_OASetProperty @email, 'SendEncrypted', 1

    -- Send the encrypted email...
    EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
      END
    ELSE
      BEGIN

        PRINT 'Encrypted Email Sent!'
      END

    EXEC @hr = sp_OADestroy @mailman
    EXEC @hr = sp_OADestroy @cert1
    EXEC @hr = sp_OADestroy @cert2
    EXEC @hr = sp_OADestroy @cert3
    EXEC @hr = sp_OADestroy @email


END
GO