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

Sign and encrypt an email with S/MIME

See more Email Object Examples

Use the sender’s signing PFX and the recipient’s public RSA certificate with with-attachments.eml, which has one recipient. Supply your own signing-key password from a credential store. Render to bytes to apply both S/MIME operations without connecting to SMTP.

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("with-attachments.eml")
If (success <> 1) Then
    outFile.WriteLine(email.LastErrorText)
    WScript.Quit
End If

set signer = CreateObject("Chilkat.Cert")
success = signer.LoadPfxFile("signer.pfx","SIGNER_PASSWORD")
If (success <> 1) Then
    outFile.WriteLine(signer.LastErrorText)
    WScript.Quit
End If

set recipient = CreateObject("Chilkat.Cert")
success = recipient.LoadFromFile("recipient.cer")
If (success <> 1) Then
    outFile.WriteLine(recipient.LastErrorText)
    WScript.Quit
End If

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

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

email.SigningHashAlg = "sha256"
email.Pkcs7CryptAlg = "aes"
email.Pkcs7KeyLength = 256
email.OaepPadding = 1
email.OaepHash = "sha256"
email.OaepMgfHash = "sha256"
email.SendSigned = 1
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("signed-encrypted.eml")
If (success <> 1) Then
    outFile.WriteLine("Failed: rendered.WriteFile.")
    WScript.Quit
End If

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

outFile.Close