Go
Go
Send an Email S/MIME Encrypted
See more Email Object Examples
Demonstrates the Chilkat Email.SendEncrypted property. Set it to true to have the email sent S/MIME encrypted (the default is false). Setting this property alone is not enough — encryption requires one or more recipient certificates, supplied with AddEncryptCert or SetEncryptCert. This example loads a recipient certificate, registers it, and enables encrypted sending.
Background: To encrypt a message for someone, you need their public certificate — which is why only a
.cer file (no private key) is required here. The message is scrambled so that only the holder of the matching private key, i.e. the intended recipient, can read it. When there are multiple recipients, you add each one's certificate, and Chilkat encrypts the one-time content key separately for each so they can all decrypt the same message.Chilkat Go Downloads
success := false
// Demonstrates the Email.SendEncrypted property. Set to true to have the email sent
// S/MIME encrypted. Encryption also requires one or more recipient certificates,
// supplied via AddEncryptCert or SetEncryptCert. The default is false.
email := chilkat.NewEmail()
email.SetSubject("Encrypted email")
email.SetBody("This message will be sent S/MIME encrypted.")
email.SetFrom("alice@example.com")
email.AddTo("Bob","bob@example.com")
// Load the recipient's certificate (only the public key is needed to encrypt).
cert := chilkat.NewCert()
success = cert.LoadFromFile("qa_data/certs/recipient.cer")
if success == false {
fmt.Println(cert.LastErrorText())
email.DisposeEmail()
cert.DisposeCert()
return
}
// Supply the recipient certificate used for encryption.
success = email.AddEncryptCert(cert)
if success == false {
fmt.Println(email.LastErrorText())
email.DisposeEmail()
cert.DisposeCert()
return
}
// Request that the email be sent encrypted.
email.SetSendEncrypted(true)
fmt.Println("SendEncrypted = ", email.SendEncrypted())
// Note: Paths such as "qa_data/..." are relative local filesystem paths,
// relative to the current working directory of the running application.
email.DisposeEmail()
cert.DisposeCert()