Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

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

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "Signed email")
    CkEmail::setCkBody(email, "This message will be sent with a digital signature.")
    CkEmail::setCkFrom(email, "alice@example.com")
    CkEmail::ckAddTo(email,"Bob","bob@example.com")

    ;  Load the certificate (public) and the matching private key from separate files.
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadFromFile(cert,"qa_data/certs/signer.cer")
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkEmail::ckDispose(email)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    privKey.i = CkPrivateKey::ckCreate()
    If privKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPrivateKey::ckLoadPemFile(privKey,"qa_data/certs/signer_privkey.pem")
    If success = 0
        Debug CkPrivateKey::ckLastErrorText(privKey)
        CkEmail::ckDispose(email)
        CkCert::ckDispose(cert)
        CkPrivateKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    ;  Set the certificate and private key used to create the signature.
    success = CkEmail::ckSetSigningCert2(email,cert,privKey)
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        CkCert::ckDispose(cert)
        CkPrivateKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    CkEmail::setCkSendSigned(email, 1)

    Debug "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.


    CkEmail::ckDispose(email)
    CkCert::ckDispose(cert)
    CkPrivateKey::ckDispose(privKey)


    ProcedureReturn
EndProcedure