Sample code for 30+ languages & platforms
DataFlex

Set the Signing Certificate and Private Key

See more Email Object Examples

Demonstrates the Chilkat Email.SetSigningCert2 method, which explicitly sets the certificate and its corresponding private key as separate objects for sending digitally signed email. This example loads a .cer certificate and a PEM private key, sets both, and enables signed sending.

Background: When your signing certificate and its private key are stored separately — a public .cer plus a PEM key file — rather than combined in a PFX, SetSigningCert2 accepts the two objects individually. It is the two-object counterpart to SetSigningCert, which takes a single certificate that already carries its private key.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    Variant vCert
    Handle hoCert
    Variant vPrivKey
    Handle hoPrivKey
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the SetSigningCert2 method, which explicitly sets the certificate and its
    //  corresponding private key (as separate objects) for sending digitally signed email.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Signed email"
    Set ComBody Of hoEmail To "This message will be sent with a digital signature."
    Set ComFrom Of hoEmail To "alice@example.com"
    Get ComAddTo Of hoEmail "Bob" "bob@example.com" To iSuccess

    //  Load the certificate (public) and the matching private key from separate files.
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadFromFile Of hoCert "qa_data/certs/signer.cer" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End
    Get ComLoadPemFile Of hoPrivKey "qa_data/certs/signer_privkey.pem" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPrivKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Set the certificate and private key used to create the signature.
    Get pvComObject of hoCert to vCert
    Get pvComObject of hoPrivKey to vPrivKey
    Get ComSetSigningCert2 Of hoEmail vCert vPrivKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Set ComSendSigned Of hoEmail To True

    Showln "Signing certificate and private key set."

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


End_Procedure