SQL Server
SQL Server
Set the Decryption Certificate for an Email
See more Email Object Examples
Demonstrates the Chilkat Email.SetDecryptCert method, which explicitly provides a certificate (with access to its private key) for decrypting a received encrypted email. Set the certificate before loading the encrypted message so Chilkat can decrypt it automatically. This example loads the certificate from a PFX, sets it, and then loads an encrypted email.
Background: Decrypting S/MIME email requires the recipient's private key. On Windows and macOS, Chilkat can find an installed certificate automatically, but when the key lives in a standalone PFX file you provide it explicitly with
SetDecryptCert. A PFX (PKCS#12) bundles the certificate and its private key in one password-protected file. If the certificate and key are in separate files, use SetDecryptCert2 instead.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 SetDecryptCert method, which explicitly provides a certificate (with
-- access to its private key) for decrypting a received encrypted email. Set the cert
-- before loading the encrypted email so it can be decrypted automatically.
DECLARE @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Load a certificate together with its private key from a PFX file.
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'qa_data/certs/decryption.pfx', 'pfx_password'
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
-- Provide the certificate to use for decryption.
EXEC sp_OAMethod @email, 'SetDecryptCert', @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
-- Load the encrypted email; Chilkat decrypts it using the certificate.
EXEC sp_OAMethod @email, 'LoadEml', @success OUT, 'qa_data/eml/encrypted.eml'
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
EXEC sp_OAGetProperty @email, 'ReceivedEncrypted', @iTmp0 OUT
PRINT 'ReceivedEncrypted = ' + @iTmp0
EXEC sp_OAGetProperty @email, 'Decrypted', @iTmp0 OUT
PRINT 'Decrypted = ' + @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