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

Xojo Plugin
Dim success As Boolean
success = False

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim email As New Chilkat.Email
success = email.LoadEml("with-attachments.eml")
If (success <> True) Then
    System.DebugLog(email.LastErrorText)
    Return
End If

Dim signer As New Chilkat.Cert
success = signer.LoadPfxFile("signer.pfx","SIGNER_PASSWORD")
If (success <> True) Then
    System.DebugLog(signer.LastErrorText)
    Return
End If

Dim recipient As New Chilkat.Cert
success = recipient.LoadFromFile("recipient.cer")
If (success <> True) Then
    System.DebugLog(recipient.LastErrorText)
    Return
End If

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

success = email.SetEncryptCert(recipient)
If (success <> True) Then
    System.DebugLog(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 New Chilkat.MailMan
Dim rendered As New Chilkat.BinData
success = mail.RenderToMimeBd(email,rendered)
If (success <> True) Then
    System.DebugLog(mail.LastErrorText)
    Return
End If

success = rendered.WriteFile("signed-encrypted.eml")
If (success <> True) Then
    System.DebugLog("Failed: rendered.WriteFile.")
    Return
End If

System.DebugLog("Saved signed-encrypted.eml.")