Sample code for 30+ languages & platforms
SQL Server

Add a PFX Source for Decrypting Email

See more Email Object Examples

Demonstrates the Chilkat Email.AddPfxSourceFile method, which adds a PFX file to the internal list of certificate and private-key sources used during decryption. Call it once per PFX file; the second argument is the PFX password. This example registers a PFX source, then loads an encrypted email so Chilkat can decrypt it automatically. AddPfxSourceFile may be called one or more times, and can be called before receiving the email from an IMAP or POP3 server as well as before loading an encrypted email from a .eml file.

Background: To read an encrypted (S/MIME) email you need the recipient's private key. On Windows, Chilkat automatically searches the system certificate stores, and on macOS the Keychain — so if the key is already installed, no extra step is needed. When the private key lives in a standalone PFX file instead (common on Linux or in server deployments), AddPfxSourceFile tells Chilkat where to find it. A PFX (PKCS#12) bundles the certificate and its private key in one password-protected file.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the AddPfxSourceFile method, which adds a PFX file to the internal list of
    --  certificate and private-key sources used during decryption.  Call it once per PFX file;
    --  the 2nd argument is the PFX password.

    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Provide a PFX containing the private key needed to decrypt the message.  AddPfxSourceFile
    --  may be called one or more times, and should be called before the encrypted email is
    --  obtained -- that is, before receiving it from an IMAP or POP3 server, or (as shown here)
    --  before loading it from a .eml file -- so Chilkat can decrypt it automatically.
    EXEC sp_OAMethod @email, 'AddPfxSourceFile', @success OUT, 'qa_data/certs/decryption.pfx', 'pfx_password'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    --  Load an encrypted email; Chilkat uses the PFX source to decrypt it.
    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
        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


END
GO