Swift
Swift
Find Certificate for Email Encryption
Demonstrates finding the recipient's certificate in the Windows certificate store and using it to send encrypted email.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let mailman = CkoMailMan()!
// Set the SMTP server.
mailman.smtpHost = "smtp.example.com"
// Create a new email object
let email = CkoEmail()!
email.subject = "This email is encrypted"
email.body = "This is a digitally encrypted mail"
email.from = "Joe <joe@example.com>"
// Emails are encrypted using the recipient's certificate.
var recipientEmailAddr: String? = "jane@example2.com"
email.add(to: "Jane", emailAddress: recipientEmailAddr)
// Indicate that the email is to be sent encrypted.
email.sendEncrypted = true
// This example demonstrates finding the email encryption certificate
// on a Windows system where the certificate is stored in the Windows
// certificate store.
let cert = CkoCert()!
// The recipient's certificate is used to encrypt.
// (Because the recipient is the only one in possession of the private key to decrypt.)
success = cert.load(byEmailAddress: recipientEmailAddr)
if success != true {
print("\(cert.lastErrorText!)")
return
}
// Specify the certificate to be used for encryption.
success = email.setEncryptCert(cert: cert)
success = mailman.sendEmail(email: email)
if success != true {
print("\(mailman.lastErrorText!)")
}
else {
print("Encrypted Mail Sent!")
}
}