Swift
Swift
S/MIME Encrypt .eml without Sending
See more Email Object Examples
Demonstrates how to encrypt an email using the recipient's digital certificate. This example just encrypts, and does not send the 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 email = CkoEmail()!
success = email.loadEml(mimePath: "c:/temp/email/unencrypted.eml")
if success == false {
print("\(email.lastErrorText!)")
return
}
// The email content is encrypted using AES with a 256-bit key, operating in GCM mode, which provides authenticated encryption.
email.pkcs7CryptAlg = "aes-gcm"
email.pkcs7KeyLength = 256
email.oaepPadding = true
email.oaepHash = "sha256"
email.oaepMgfHash = "sha256"
let cert = CkoCert()!
success = cert.load(fromFile: "c/temps/cert/recipient.cer")
if success == false {
print("\(cert.lastErrorText!)")
return
}
email.sendEncrypted = true
email.setEncryptCert(cert: cert)
let sbSmime = CkoStringBuilder()!
// The mailman object applies the encryption by rendering the email according to the instructions (property settings) provided in the email object.
// No email is sent.
let mailman = CkoMailMan()!
success = mailman.render(toMimeSb: email, renderedMime: sbSmime)
if success == false {
print("\(mailman.lastErrorText!)")
return
}
success = sbSmime.writeFile(path: "c:/temp/encryptedEmail.eml", charset: "utf-8", emitBom: false)
if success == false {
print("\(mailman.lastErrorText!)")
return
}
print("Success!")
}