Sample code for 30+ languages & platforms
Lianja

Set Cert and Private Key for S/MIME Decryption

See more IMAP Examples

Demonstrates the Chilkat Imap.SetDecryptCert2 method, which specifies a certificate together with a separately supplied private key for decrypting S/MIME messages. The first argument is the Cert and the second is the PrivateKey. This example loads the certificate and key from separate files.

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

Background: Sometimes the certificate (the public part) and the private key are stored separately — a .cer alongside a PEM key file, for instance. This method pairs them explicitly, whereas SetDecryptCert expects a single Cert that already provides its private key. After the pairing, subsequently downloaded S/MIME messages are decrypted automatically.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

//  Demonstrates the Imap.SetDecryptCert2 method, which specifies a certificate together with a
//  separately supplied private key for decrypting S/MIME messages.  The 1st argument is the
//  Cert and the 2nd is the PrivateKey.

loImap = createobject("CkImap")

loImap.Ssl = .T.
loImap.Port = 993

llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llSuccess = loImap.Login("user@example.com","myPassword")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

//  Load the certificate and its private key from separate files.
loCert = createobject("CkCert")
llSuccess = loCert.LoadFromFile("qa_data/smime.cer")
if (llSuccess = .F.) then
    ? loCert.LastErrorText
    release loImap
    release loCert
    return
endif

loPrivKey = createobject("CkPrivateKey")
llSuccess = loPrivKey.LoadPemFile("qa_data/smime_privkey.pem")
if (llSuccess = .F.) then
    ? loPrivKey.LastErrorText
    release loImap
    release loCert
    release loPrivKey
    return
endif

//  Use the cert and key to automatically decrypt subsequently downloaded S/MIME messages.
llSuccess = loImap.SetDecryptCert2(loCert,loPrivKey)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loCert
    release loPrivKey
    return
endif

llSuccess = loImap.Disconnect()
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loCert
    release loPrivKey
    return
endif



release loImap
release loCert
release loPrivKey