Sample code for 30+ languages & platforms
SQL Server

Provide a Certificate Vault to MailMan

See more POP3 Examples

Demonstrates the Chilkat MailMan.UseCertVault method, which provides an XML certificate vault to be searched for certificates and private keys when MailMan performs operations such as encryption, decryption, signing, or signature verification. This example builds a vault from a PFX, attaches it, and fetches a message that Chilkat can decrypt using the vault.

Background: A certificate vault is a portable, in-memory store of certificates and private keys. Rather than wiring up a specific certificate for each operation, you load your credentials into one XmlCertVault and hand it to MailMan, which then searches it automatically whenever a key is needed. This is especially useful on platforms without an OS certificate store, or when one application handles several identities.

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 MailMan.UseCertVault method, which provides an XML certificate vault to
    --  be searched for certificates and private keys when MailMan performs operations such as
    --  encryption, decryption, signing, or signature verification.

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

    --  Configure the POP3 server connection.
    EXEC sp_OASetProperty @mailman, 'MailHost', 'pop.example.com'
    EXEC sp_OASetProperty @mailman, 'MailPort', 995
    EXEC sp_OASetProperty @mailman, 'PopSsl', 1
    EXEC sp_OASetProperty @mailman, 'PopUsername', 'user@example.com'
    EXEC sp_OASetProperty @mailman, 'PopPassword', 'myPassword'

    --  Build a certificate vault from a PFX (certificate + private key).
    DECLARE @vault int
    EXEC @hr = sp_OACreate 'Chilkat.XmlCertVault', @vault OUT

    EXEC sp_OAMethod @vault, 'AddPfxFile', @success OUT, 'qa_data/certs/certs.pfx', 'pfx_password'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @vault, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @vault
        RETURN
      END

    --  Make the vault available to MailMan for crypto operations.
    EXEC sp_OAMethod @mailman, 'UseCertVault', @success OUT, @vault
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @vault
        RETURN
      END

    --  Fetch a message; Chilkat draws on the vault if a private key is needed to decrypt it.
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    EXEC sp_OAMethod @mailman, 'FetchOne', @success OUT, 0, 0, 1, @email
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @vault
        EXEC @hr = sp_OADestroy @email
        RETURN
      END


    EXEC sp_OAGetProperty @email, 'Decrypted', @iTmp0 OUT
    PRINT 'Decrypted = ' + @iTmp0

    --  Note: The path "qa_data/certs/certs.pfx" is a relative local filesystem path,
    --  relative to the current working directory of the running application.

    --  Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
    --  connects and authenticates -- using the property settings above -- whenever a server
    --  operation requires it.  Calling the explicit connect/authenticate methods can still be
    --  helpful to determine whether a failure occurs while connecting or while authenticating.

    EXEC @hr = sp_OADestroy @mailman
    EXEC @hr = sp_OADestroy @vault
    EXEC @hr = sp_OADestroy @email


END
GO