Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
oleobject loo_Cert
oleobject loo_PrivKey

li_Success = 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.

loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Imap.Ssl = 1
loo_Imap.Port = 993

li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login("user@example.com","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

//  Load the certificate and its private key from separate files.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadFromFile("qa_data/smime.cer")
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Imap
    destroy loo_Cert
    return
end if

loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_PrivKey.LoadPemFile("qa_data/smime_privkey.pem")
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_Imap
    destroy loo_Cert
    destroy loo_PrivKey
    return
end if

//  Use the cert and key to automatically decrypt subsequently downloaded S/MIME messages.
li_Success = loo_Imap.SetDecryptCert2(loo_Cert,loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Cert
    destroy loo_PrivKey
    return
end if

li_Success = loo_Imap.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Cert
    destroy loo_PrivKey
    return
end if



destroy loo_Imap
destroy loo_Cert
destroy loo_PrivKey