Sample code for 30+ languages & platforms
SQL Server

Set the Certificate for Decrypting S/MIME Email

See more IMAP Examples

Demonstrates the Chilkat Imap.SetDecryptCert method, which specifies the certificate used to decrypt S/MIME messages downloaded by this Imap object. The only argument is a Cert that has access to its private key. This example loads the certificate from a PFX file.

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

Background: An S/MIME-encrypted message can only be read with the recipient's private key. Once a decrypt certificate is set, Chilkat decrypts matching messages automatically as they are fetched, so your code works with plaintext Email objects. Use SetDecryptCert2 instead when the certificate object does not itself carry the private key.

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.SetDecryptCert method, which specifies the certificate used to decrypt
    --  S/MIME messages downloaded by this Imap object.  The only argument is a Cert that has access
    --  to its private key.

    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

    --  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)

    --  Load a certificate (with 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/smime.pfx', @pfxPassword
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    --  Use it to automatically decrypt S/MIME messages that are subsequently downloaded.
    EXEC sp_OAMethod @imap, 'SetDecryptCert', @success OUT, @cert
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    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 @cert
        RETURN
      END

    EXEC @hr = sp_OADestroy @imap
    EXEC @hr = sp_OADestroy @cert


END
GO