SQL Server
SQL Server
Send an Email S/MIME Encrypted
See more Email Object Examples
Demonstrates the Chilkat Email.SendEncrypted property. Set it to true to have the email sent S/MIME encrypted (the default is false). Setting this property alone is not enough — encryption requires one or more recipient certificates, supplied with AddEncryptCert or SetEncryptCert. This example loads a recipient certificate, registers it, and enables encrypted sending.
Background: To encrypt a message for someone, you need their public certificate — which is why only a
.cer file (no private key) is required here. The message is scrambled so that only the holder of the matching private key, i.e. the intended recipient, can read it. When there are multiple recipients, you add each one's certificate, and Chilkat encrypts the one-time content key separately for each so they can all decrypt the same message.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Demonstrates the Email.SendEncrypted property. Set to true to have the email sent
-- S/MIME encrypted. Encryption also requires one or more recipient certificates,
-- supplied via AddEncryptCert or SetEncryptCert. The default is false.
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
-- Supply the recipient certificate used for encryption.
EXEC sp_OAMethod @email, 'AddEncryptCert', @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 that the email be sent encrypted.
EXEC sp_OASetProperty @email, 'SendEncrypted', 1
EXEC sp_OAGetProperty @email, 'SendEncrypted', @iTmp0 OUT
PRINT 'SendEncrypted = ' + @iTmp0
-- Note: Paths such as "qa_data/..." are relative local filesystem paths,
-- relative to the current working directory of the running application.
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @cert
END
GO