Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  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.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End

    //  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.
    Get ComAddPfxSourceFile Of hoEmail "qa_data/certs/decryption.pfx" "pfx_password" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Load an encrypted email; Chilkat uses the PFX source to decrypt it.
    Get ComLoadEml Of hoEmail "qa_data/eml/encrypted.eml" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComReceivedEncrypted Of hoEmail To bTemp1
    Showln "ReceivedEncrypted = " bTemp1
    Get ComDecrypted Of hoEmail To bTemp1
    Showln "Decrypted = " bTemp1

    //  Note: Paths such as "qa_data/..." are relative local filesystem paths,
    //  relative to the current working directory of the running application.


End_Procedure