PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkEmail.pb"
IncludeFile "CkCert.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the SetEncryptCert method, which sets the explicit recipient encryption
; certificate for sending an encrypted email.
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkEmail::setCkSubject(email, "Encrypted email")
CkEmail::setCkBody(email, "This message will be sent S/MIME encrypted.")
CkEmail::setCkFrom(email, "alice@example.com")
CkEmail::ckAddTo(email,"Bob","bob@example.com")
; Load the recipient's certificate (only the public key is needed to encrypt).
cert.i = CkCert::ckCreate()
If cert.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkCert::ckLoadFromFile(cert,"qa_data/certs/recipient.cer")
If success = 0
Debug CkCert::ckLastErrorText(cert)
CkEmail::ckDispose(email)
CkCert::ckDispose(cert)
ProcedureReturn
EndIf
; Set the explicit recipient encryption certificate.
success = CkEmail::ckSetEncryptCert(email,cert)
If success = 0
Debug CkEmail::ckLastErrorText(email)
CkEmail::ckDispose(email)
CkCert::ckDispose(cert)
ProcedureReturn
EndIf
; Request encrypted sending.
CkEmail::setCkSendEncrypted(email, 1)
Debug "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.
CkEmail::ckDispose(email)
CkCert::ckDispose(cert)
ProcedureReturn
EndProcedure