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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vEmail
    Handle hoEmail
    Variant vSigner
    Handle hoSigner
    Variant vRecipient
    Handle hoRecipient
    Handle hoMail
    Variant vRendered
    Handle hoRendered
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Get ComLoadEml Of hoEmail "with-attachments.eml" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatCert)) To hoSigner
    If (Not(IsComObjectCreated(hoSigner))) Begin
        Send CreateComObject of hoSigner
    End
    Get ComLoadPfxFile Of hoSigner "signer.pfx" "SIGNER_PASSWORD" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoSigner To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatCert)) To hoRecipient
    If (Not(IsComObjectCreated(hoRecipient))) Begin
        Send CreateComObject of hoRecipient
    End
    Get ComLoadFromFile Of hoRecipient "recipient.cer" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRecipient To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoSigner to vSigner
    Get ComSetSigningCert Of hoEmail vSigner To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoRecipient to vRecipient
    Get ComSetEncryptCert Of hoEmail vRecipient To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Set ComSigningHashAlg Of hoEmail To "sha256"
    Set ComPkcs7CryptAlg Of hoEmail To "aes"
    Set ComPkcs7KeyLength Of hoEmail To 256
    Set ComOaepPadding Of hoEmail To True
    Set ComOaepHash Of hoEmail To "sha256"
    Set ComOaepMgfHash Of hoEmail To "sha256"
    Set ComSendSigned Of hoEmail To True
    Set ComSendEncrypted Of hoEmail To True
    Get Create (RefClass(cComChilkatMailMan)) To hoMail
    If (Not(IsComObjectCreated(hoMail))) Begin
        Send CreateComObject of hoMail
    End
    Get Create (RefClass(cComChilkatBinData)) To hoRendered
    If (Not(IsComObjectCreated(hoRendered))) Begin
        Send CreateComObject of hoRendered
    End
    Get pvComObject of hoEmail to vEmail
    Get pvComObject of hoRendered to vRendered
    Get ComRenderToMimeBd Of hoMail vEmail vRendered To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComWriteFile Of hoRendered "signed-encrypted.eml" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Failed: rendered.WriteFile."
        Procedure_Return
    End

    Showln "Saved signed-encrypted.eml."


End_Procedure