Sample code for 30+ languages & platforms
PowerBuilder

Set the Decryption Certificate and Private Key

See more POP3 Examples

Demonstrates the Chilkat MailMan.SetDecryptCert2 method, which explicitly specifies both the certificate and its associated private key, as separate objects, to use when decrypting S/MIME encrypted email. This example loads a .cer certificate and a PEM private key, registers both, and fetches a message Chilkat can decrypt.

Background: Credentials are not always packaged together. When the certificate is a standalone .cer and the private key is a separate PEM (or other) file, SetDecryptCert2 takes the two objects individually. It is the two-object counterpart to SetDecryptCert, which expects a single certificate that already carries its private key (such as one loaded from a PFX).

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mailman
oleobject loo_Cert
oleobject loo_PrivKey
oleobject loo_Email

li_Success = 0

//  Demonstrates the MailMan.SetDecryptCert2 method, which explicitly specifies both the
//  certificate and its associated private key (as separate objects) to use when decrypting
//  S/MIME encrypted email.

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

//  Configure the POP3 server connection.
loo_Mailman.MailHost = "pop.example.com"
loo_Mailman.MailPort = 995
loo_Mailman.PopSsl = 1
loo_Mailman.PopUsername = "user@example.com"
loo_Mailman.PopPassword = "myPassword"

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

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

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

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

//  Specify the certificate and private key to use when decrypting downloaded email.
li_Success = loo_Mailman.SetDecryptCert2(loo_Cert,loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    destroy loo_Cert
    destroy loo_PrivKey
    return
end if

//  Fetch a message; if it is encrypted, Chilkat decrypts it using the certificate and key.
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Mailman.FetchOne(0,0,1,loo_Email)
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    destroy loo_Cert
    destroy loo_PrivKey
    destroy loo_Email
    return
end if

Write-Debug "Decrypted = " + string(loo_Email.Decrypted)

//  Note: Paths such as "qa_data/..." are relative local filesystem paths,
//  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.


destroy loo_Mailman
destroy loo_Cert
destroy loo_PrivKey
destroy loo_Email