Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImap
LOCAL loCert
LOCAL loPrivKey

lnSuccess = 0

*  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('Chilkat.Imap')

loImap.Ssl = 1
loImap.Port = 993

lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.Login("user@example.com","myPassword")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

*  Load the certificate and its private key from separate files.
loCert = CreateObject('Chilkat.Cert')
lnSuccess = loCert.LoadFromFile("qa_data/smime.cer")
IF (lnSuccess = 0) THEN
    ? loCert.LastErrorText
    RELEASE loImap
    RELEASE loCert
    CANCEL
ENDIF

loPrivKey = CreateObject('Chilkat.PrivateKey')
lnSuccess = loPrivKey.LoadPemFile("qa_data/smime_privkey.pem")
IF (lnSuccess = 0) THEN
    ? loPrivKey.LastErrorText
    RELEASE loImap
    RELEASE loCert
    RELEASE loPrivKey
    CANCEL
ENDIF

*  Use the cert and key to automatically decrypt subsequently downloaded S/MIME messages.
lnSuccess = loImap.SetDecryptCert2(loCert,loPrivKey)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loCert
    RELEASE loPrivKey
    CANCEL
ENDIF

lnSuccess = loImap.Disconnect()
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loCert
    RELEASE loPrivKey
    CANCEL
ENDIF

RELEASE loImap
RELEASE loCert
RELEASE loPrivKey