Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oEmail
LOCAL oCert

nSuccess := 0

//  Demonstrates the SetEncryptCert method, which sets the explicit recipient encryption
//  certificate for sending an encrypted email.

oEmail := CreateObject("Chilkat.Email")
oEmail:Subject := "Encrypted email"
oEmail:Body := "This message will be sent S/MIME encrypted."
oEmail:From := "alice@example.com"
oEmail:AddTo("Bob", "bob@example.com")

//  Load the recipient's certificate (only the public key is needed to encrypt).
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadFromFile("qa_data/certs/recipient.cer")
IF (nSuccess == 0)
    ? oCert:LastErrorText
    oEmail:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Set the explicit recipient encryption certificate.
nSuccess := oEmail:SetEncryptCert(oCert)
IF (nSuccess == 0)
    ? oEmail:LastErrorText
    oEmail:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Request encrypted sending.
oEmail:SendEncrypted := 1

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

oEmail:destroy()
oCert:destroy()