Sample code for 30+ languages & platforms
VBScript 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 VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

set email = CreateObject("Chilkat.Email")
success = email.LoadEml("message.eml")
If (success <> 1) Then
    outFile.WriteLine(email.LastErrorText)
    WScript.Quit
End If

'  Use the recipient's public encryption certificate. Its private key stays with the recipient.
set cert = CreateObject("Chilkat.Cert")
success = cert.LoadFromFile("recipient.cer")
If (success <> 1) Then
    outFile.WriteLine(cert.LastErrorText)
    WScript.Quit
End If

success = email.SetEncryptCert(cert)
If (success <> 1) Then
    outFile.WriteLine(email.LastErrorText)
    WScript.Quit
End If

email.Pkcs7CryptAlg = "aes-gcm"
email.Pkcs7KeyLength = 256
email.OaepPadding = 1
email.OaepHash = "sha256"
email.OaepMgfHash = "sha256"
email.SendEncrypted = 1
set mail = CreateObject("Chilkat.MailMan")
set rendered = CreateObject("Chilkat.BinData")
success = mail.RenderToMimeBd(email,rendered)
If (success <> 1) Then
    outFile.WriteLine(mail.LastErrorText)
    WScript.Quit
End If

success = rendered.WriteFile("encrypted.eml")
If (success <> 1) Then
    outFile.WriteLine("Failed: rendered.WriteFile.")
    WScript.Quit
End If

outFile.WriteLine("Saved encrypted.eml.")

outFile.Close