Sample code for 30+ languages & platforms
DataFlex

Specify a Certificate for Encrypted Email

See more Email Object Examples

Demonstrates the Chilkat Email.AddEncryptCert method, which explicitly specifies a certificate to use when sending encrypted email. Call it once per recipient certificate; ClearEncryptCerts clears the list. This example loads a recipient certificate, registers it, and enables encrypted sending.

Background: When encrypting to multiple people, each recipient needs to be able to decrypt with their own private key. S/MIME handles this by encrypting the message's one-time content key separately under each recipient's public certificate and including all of those wrapped keys in the message. AddEncryptCert is how you build that recipient list — one call per certificate — giving explicit control over exactly whose certificates are used rather than relying on automatic lookup.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

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

    Move False To iSuccess

    //  Demonstrates the AddEncryptCert method, which explicitly specifies a certificate for
    //  sending encrypted email.  Call it once per recipient certificate.  Use ClearEncryptCerts
    //  to clear the list.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Encrypted email"
    Set ComBody Of hoEmail To "Encrypted to the specified recipient certificate(s)."
    Set ComFrom Of hoEmail To "alice@example.com"
    Get ComAddTo Of hoEmail "Bob" "bob@example.com" To iSuccess

    //  Load a recipient certificate (public key) and add it to the encryption cert list.
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadFromFile Of hoCert "qa_data/certs/recipient.cer" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoCert to vCert
    Get ComAddEncryptCert Of hoEmail vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Request encrypted sending.
    Set ComSendEncrypted Of hoEmail To True

    Showln "Added the recipient's encryption certificate."

    //  Note: The path "qa_data/certs/recipient.cer" is a relative local filesystem path,
    //  relative to the current working directory of the running application.


End_Procedure