Sample code for 30+ languages & platforms
SQL Server

Set the Decryption Certificate for MailMan

See more POP3 Examples

Demonstrates the Chilkat MailMan.SetDecryptCert method, which explicitly specifies the certificate to use when decrypting encrypted email. The certificate must correspond to the recipient certificate used to encrypt the message and must have access to its private key. This example loads the certificate from a PFX, registers it, and fetches a message that Chilkat can then decrypt.

Background: Decrypting S/MIME mail requires the recipient's private key, so the certificate you supply must carry it — which is why a PFX (certificate + private key in one password-protected file) is used here. Chilkat can find installed certificates automatically on Windows and macOS; SetDecryptCert is how you point it at a specific one instead. If the certificate and key live in separate files, use SetDecryptCert2.

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.SetDecryptCert method, which explicitly specifies the certificate
    --  to use when decrypting encrypted email.  The certificate must correspond to the recipient
    --  certificate used to encrypt the message, and must have access to its private key.

    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'

    --  Load the 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 @mailman
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    --  Specify the certificate to use when decrypting downloaded email.
    EXEC sp_OAMethod @mailman, 'SetDecryptCert', @success OUT, @cert
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    --  Fetch a message; if it is encrypted, Chilkat decrypts it using this certificate.
    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 @cert
        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: The path "qa_data/certs/decryption.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 @cert
    EXEC @hr = sp_OADestroy @email


END
GO