Sample code for 30+ languages & platforms
B4X Requires Chilkat v11.0.0+

Create an encrypted S/MIME file without SMTP

See more Email Object Examples

Encrypt a saved draft for a recipient with an RSA encryption certificate. This example uses AES-256-GCM for the content and RSA-OAEP for the content-encryption key. Confirm these choices are supported by the receiving mail client. RenderToMimeBd produces the protected bytes without submitting mail.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim email As ChilkatEmail
email.Initialize
success = email.LoadEml("message.eml")
If success <> True Then
    Log(email.LastErrorText)
    Return
End If

'  Use the recipient's public encryption certificate. Its private key stays with the recipient.
Dim cert As ChilkatCert
cert.Initialize("cert")
success = cert.LoadFromFile("recipient.cer")
If success <> True Then
    Log(cert.LastErrorText)
    Return
End If

success = email.SetEncryptCert(cert)
If success <> True Then
    Log(email.LastErrorText)
    Return
End If

email.Pkcs7CryptAlg = "aes-gcm"
email.Pkcs7KeyLength = 256
email.OaepPadding = True
email.OaepHash = "sha256"
email.OaepMgfHash = "sha256"
email.SendEncrypted = True
Dim mail As ChilkatMailMan
mail.Initialize("mail")
Dim rendered As ChilkatBinData
rendered.Initialize
success = mail.RenderToMimeBd(email, rendered)
If success <> True Then
    Log(mail.LastErrorText)
    Return
End If

success = rendered.WriteFile("encrypted.eml")
If success <> True Then
    Log("Failed: rendered.WriteFile.")
    Return
End If

Log("Saved encrypted.eml.")