PureBasic
PureBasic
Send Encrypted Email using Certificate in Apple Keychain
See more Apple Keychain Examples
Send encrypted (S/MIME) email using a certificate found in the Apple Keychain.Note: This example requires Chilkat v10.1.2 or later.
Chilkat PureBasic Downloads
IncludeFile "CkCert.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkSecrets.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkMailMan.pb"
Procedure ChilkatExample()
success.i = 0
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkEmail::setCkSubject(email, "This email is encrypted")
CkEmail::setCkBody(email, "This is a S/MIME encrypted mail")
CkEmail::setCkFrom(email, "Joe <joe@example.com>")
; Emails are encrypted using the recipient's certificate.
recipientEmailAddr.s = "jane@example2.com"
CkEmail::ckAddTo(email,"",recipientEmailAddr)
; Indicate that the email is to be sent encrypted.
CkEmail::setCkSendEncrypted(email, 1)
cert.i = CkCert::ckCreate()
If cert.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; -----------------------------------------------
; This example requires Chilkat v10.1.2 or later.
; -----------------------------------------------
; The recipient's certificate is used to encrypt.
; This will load the certificate from the Apple Keychain.
success = CkCert::ckLoadByEmailAddress(cert,recipientEmailAddr)
If success <> 1
Debug CkCert::ckLastErrorText(cert)
CkEmail::ckDispose(email)
CkCert::ckDispose(cert)
ProcedureReturn
EndIf
; Specify the certificate to be used for encryption.
success = CkEmail::ckSetEncryptCert(email,cert)
CkEmail::setCkSendEncrypted(email, 1)
mailman.i = CkMailMan::ckCreate()
If mailman.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkMailMan::setCkSmtpHost(mailman, "smtp.example.com")
CkMailMan::setCkSmtpUsername(mailman, "joe@example.com")
CkMailMan::setCkSmtpPort(mailman, 587)
CkMailMan::setCkStartTLS(mailman, 1)
; We'll get the SMTP password from the Apple Keychain.
; See how we originally saved the email credentials to the Keychain here:
; Save Email Credentials in Apple Keychain or Windows Credentials Manager
secrets.i = CkSecrets::ckCreate()
If secrets.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; On Windows, this is the Windows Credentials Manager
; On MacOS/iOS, it is the Apple Keychain
CkSecrets::setCkLocation(secrets, "local_manager")
; Specify the name of the secret.
; service and username are required.
; appName and domain are optional.
; Note: The values are arbitrary and can be anything you want.
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"appName","MyEmailApp")
CkJsonObject::ckUpdateString(json,"service","SMTP")
CkJsonObject::ckUpdateString(json,"domain","example.com")
CkJsonObject::ckUpdateString(json,"username","joe@example.com")
CkMailMan::setCkSmtpPassword(mailman, CkSecrets::ckGetSecretStr(secrets,json))
; Send the encrypted email.
; The email is encrypted and sent within the SendEmail function.
success = CkMailMan::ckSendEmail(mailman,email)
If success <> 1
Debug CkMailMan::ckLastErrorText(mailman)
CkEmail::ckDispose(email)
CkCert::ckDispose(cert)
CkMailMan::ckDispose(mailman)
CkSecrets::ckDispose(secrets)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
Debug "Encrypted email sent!"
CkEmail::ckDispose(email)
CkCert::ckDispose(cert)
CkMailMan::ckDispose(mailman)
CkSecrets::ckDispose(secrets)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndProcedure