Sample code for 30+ languages & platforms
B4X 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 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("with-attachments.eml")
If success <> True Then
    Log(email.LastErrorText)
    Return
End If

Dim signer As ChilkatCert
signer.Initialize("signer")
success = signer.LoadPfxFile("signer.pfx", "SIGNER_PASSWORD")
If success <> True Then
    Log(signer.LastErrorText)
    Return
End If

Dim recipient As ChilkatCert
recipient.Initialize("recipient")
success = recipient.LoadFromFile("recipient.cer")
If success <> True Then
    Log(recipient.LastErrorText)
    Return
End If

success = email.SetSigningCert(signer)
If success <> True Then
    Log(email.LastErrorText)
    Return
End If

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

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

Log("Saved signed-encrypted.eml.")