Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    Variant vCert
    Handle hoCert
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

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

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

    //  Load a certificate together with its private key from a PFX file.
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadPfxFile Of hoCert "qa_data/certs/decryption.pfx" "pfx_password" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Provide the certificate to use for decryption.
    Get pvComObject of hoCert to vCert
    Get ComSetDecryptCert Of hoEmail vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Load the encrypted email; Chilkat decrypts it using the certificate.
    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