Sample code for 30+ languages & platforms
PowerBuilder

Add a PFX Source for Decrypting Email

See more Email Object Examples

Demonstrates the Chilkat Email.AddPfxSourceFile method, which adds a PFX file to the internal list of certificate and private-key sources used during decryption. Call it once per PFX file; the second argument is the PFX password. This example registers a PFX source, then loads an encrypted email so Chilkat can decrypt it automatically. AddPfxSourceFile may be called one or more times, and can be called before receiving the email from an IMAP or POP3 server as well as before loading an encrypted email from a .eml file.

Background: To read an encrypted (S/MIME) email you need the recipient's private key. On Windows, Chilkat automatically searches the system certificate stores, and on macOS the Keychain — so if the key is already installed, no extra step is needed. When the private key lives in a standalone PFX file instead (common on Linux or in server deployments), AddPfxSourceFile tells Chilkat where to find it. A PFX (PKCS#12) bundles the certificate and its private key in one password-protected file.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Email

li_Success = 0

//  Demonstrates the AddPfxSourceFile method, which adds a PFX file to the internal list of
//  certificate and private-key sources used during decryption.  Call it once per PFX file;
//  the 2nd argument is the PFX password.

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

//  Provide a PFX containing the private key needed to decrypt the message.  AddPfxSourceFile
//  may be called one or more times, and should be called before the encrypted email is
//  obtained -- that is, before receiving it from an IMAP or POP3 server, or (as shown here)
//  before loading it from a .eml file -- so Chilkat can decrypt it automatically.
li_Success = loo_Email.AddPfxSourceFile("qa_data/certs/decryption.pfx","pfx_password")
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    return
end if

//  Load an encrypted email; Chilkat uses the PFX source to decrypt it.
li_Success = loo_Email.LoadEml("qa_data/eml/encrypted.eml")
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    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