Sample code for 30+ languages & platforms
PowerBuilder

Set the Decryption Certificate for an Email

See more Email Object Examples

Demonstrates the Chilkat Email.SetDecryptCert method, which explicitly provides a certificate (with access to its private key) for decrypting a received encrypted email. Set the certificate before loading the encrypted message so Chilkat can decrypt it automatically. This example loads the certificate from a PFX, sets it, and then loads an encrypted email.

Background: Decrypting S/MIME email requires the recipient's private key. On Windows and macOS, Chilkat can find an installed certificate automatically, but when the key lives in a standalone PFX file you provide it explicitly with SetDecryptCert. A PFX (PKCS#12) bundles the certificate and its private key in one password-protected file. If the certificate and key are in separate files, use SetDecryptCert2 instead.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Email
oleobject loo_Cert

li_Success = 0

//  Demonstrates the SetDecryptCert method, which explicitly provides a certificate (with
//  access to its private key) for decrypting a received encrypted email.  Set the cert
//  before loading the encrypted email so it can be decrypted automatically.

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

//  Load a certificate together with its private key from a PFX file.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("qa_data/certs/decryption.pfx","pfx_password")
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Email
    destroy loo_Cert
    return
end if

//  Provide the certificate to use for decryption.
li_Success = loo_Email.SetDecryptCert(loo_Cert)
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Cert
    return
end if

//  Load the encrypted email; Chilkat decrypts it using the certificate.
li_Success = loo_Email.LoadEml("qa_data/eml/encrypted.eml")
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Cert
    return
end if

Write-Debug "ReceivedEncrypted = " + string(loo_Email.ReceivedEncrypted)
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.


destroy loo_Email
destroy loo_Cert