Sample code for 30+ languages & platforms
B4X

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 B4X Downloads

B4X
Dim success As Boolean = False

'  This example requires the Chilkat API to have been previously unlocked.
'  See Global Unlock Sample for sample code.

Dim email As ChilkatEmail
email.Initialize

success = email.LoadEml("c:/temp/email/unencrypted.eml")
If success = False Then
    Log(email.LastErrorText)
    Return
End If


'  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"

Dim cert As ChilkatCert
cert.Initialize("cert")
success = cert.LoadFromFile("c/temps/cert/recipient.cer")
If success = False Then
    Log(cert.LastErrorText)
    Return
End If


email.SendEncrypted = True
email.SetEncryptCert(cert)

Dim sbSmime As ChilkatStringBuilder
sbSmime.Initialize

'  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.
Dim mailman As ChilkatMailMan
mailman.Initialize("mailman")
success = mailman.RenderToMimeSb(email, sbSmime)
If success = False Then
    Log(mailman.LastErrorText)
    Return
End If


success = sbSmime.WriteFile("c:/temp/encryptedEmail.eml", "utf-8", False)
If success = False Then
    Log(mailman.LastErrorText)
    Return
End If


Log("Success!")