Sample code for 30+ languages & platforms
DataFlex

Set the Recipient Encryption Certificate

See more Email Object Examples

Demonstrates the Chilkat Email.SetEncryptCert method, which sets the explicit recipient encryption certificate for sending an encrypted email. This example loads the recipient's certificate, sets it, and enables encrypted sending.

Background: To encrypt a message for a recipient you need their public certificate — hence a .cer file (no private key) suffices. SetEncryptCert specifies a single recipient certificate; to encrypt for multiple recipients, use AddEncryptCert once per certificate. Either way, setting SendEncrypted to true is what actually requests encryption when the message is sent.

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 SetEncryptCert method, which sets the explicit recipient encryption
    //  certificate for sending an encrypted email.

    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 "This message will be sent S/MIME encrypted."
    Set ComFrom Of hoEmail To "alice@example.com"
    Get ComAddTo Of hoEmail "Bob" "bob@example.com" To iSuccess

    //  Load the recipient's certificate (only the public key is needed to encrypt).
    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

    //  Set the explicit recipient encryption certificate.
    Get pvComObject of hoCert to vCert
    Get ComSetEncryptCert 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 "Encryption certificate set."

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