Sample code for 30+ languages & platforms
SQL Server

Add a PFX Source (BinData) for S/MIME Decryption

See more IMAP Examples

Demonstrates the Chilkat Imap.AddPfxSourceBd method, which adds PFX data held in a BinData as a source of certificates and private keys for S/MIME processing. The first argument is the BinData and the second is the PFX password. Call it once for each additional source.

Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.

Background: When downloading encrypted (S/MIME) email, Chilkat needs the recipient's private key to decrypt it. Adding one or more PFX sources gives Chilkat a pool of keys to search; the matching key is used automatically as messages are fetched. The BinData form is handy when the PFX comes from memory — a database blob or a downloaded buffer — rather than a file on disk. On Windows and macOS the system certificate stores are also searched automatically.

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
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the Imap.AddPfxSourceBd method, which adds PFX data (from a BinData) as a
    --  source of certificates and private keys for S/MIME decryption.  The 1st argument is the
    --  BinData containing the PFX, and the 2nd is the PFX password.

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

    EXEC sp_OASetProperty @imap, 'Ssl', 1
    EXEC sp_OASetProperty @imap, 'Port', 993

    EXEC sp_OAMethod @imap, 'Connect', @success OUT, 'imap.example.com'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        RETURN
      END
    EXEC sp_OAMethod @imap, 'Login', @success OUT, 'user@example.com', 'myPassword'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        RETURN
      END

    --  Load the PFX data into a BinData.
    DECLARE @bdPfx int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdPfx OUT

    EXEC sp_OAMethod @bdPfx, 'LoadFile', @success OUT, 'qa_data/smime.pfx'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @bdPfx, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @bdPfx
        RETURN
      END

    --  The PFX password is not hard-coded in source.  Insert code here to obtain it from an
    --  interactive prompt, environment variable, or a secrets vault.
    DECLARE @pfxPassword nvarchar(4000)

    --  Add the PFX as a source for S/MIME certificates and private keys.  Call once per source.
    EXEC sp_OAMethod @imap, 'AddPfxSourceBd', @success OUT, @bdPfx, @pfxPassword
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @bdPfx
        RETURN
      END

    --  Messages downloaded after this are automatically decrypted when a matching key is found.

    EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @bdPfx
        RETURN
      END

    EXEC @hr = sp_OADestroy @imap
    EXEC @hr = sp_OADestroy @bdPfx


END
GO