SQL Server
SQL Server
Set the Recipient Encryption Certificate
See more Email Object Examples
Demonstrates the Chilkat Email.SetEncryptCert method, which sets the explicit recipient encryption certificate for sending an encrypted email. This example loads the recipient's certificate, sets it, and enables encrypted sending.
Background: To encrypt a message for a recipient you need their public certificate — hence a
.cer file (no private key) suffices. SetEncryptCert specifies a single recipient certificate; to encrypt for multiple recipients, use AddEncryptCert once per certificate. Either way, setting SendEncrypted to true is what actually requests encryption when the message is sent.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Demonstrates the SetEncryptCert method, which sets the explicit recipient encryption
-- certificate for sending an encrypted email.
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', 'Encrypted email'
EXEC sp_OASetProperty @email, 'Body', 'This message will be sent S/MIME encrypted.'
EXEC sp_OASetProperty @email, 'From', 'alice@example.com'
EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Bob', 'bob@example.com'
-- Load the recipient's certificate (only the public key is needed to encrypt).
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, 'qa_data/certs/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
-- Set the explicit recipient encryption certificate.
EXEC sp_OAMethod @email, 'SetEncryptCert', @success OUT, @cert
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- Request encrypted sending.
EXEC sp_OASetProperty @email, 'SendEncrypted', 1
PRINT 'Encryption certificate set.'
-- Note: The path "qa_data/certs/recipient.cer" is a relative local filesystem path,
-- relative to the current working directory of the running application.
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @cert
END
GO