Sample code for 30+ languages & platforms
DataFlex

Send Encrypted Email using RSAES-OAEP with AES-128 CBC and SHA256

See more SMTP Examples

Demonstrates how to send encrypted email using RSAES-OAEP with SHA256 and AES-128 content encryption.

Note: This example requires Chilkat v9.5.0.67 or greater.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vCert
    Handle hoCert
    Variant vEmail
    Handle hoEmail
    Handle hoMailman
    String sTemp1

    Move False To iSuccess

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Load an RSA-based certificate.
    // (Encrypting an email only requires the public key.  Decrypting an email requires the private key.)
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadFromFile Of hoCert "qa_data/rsaes-oaep/cert.pem" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End

    // Create a simple email.
    Set ComSubject Of hoEmail To "Sample RSAES-OAEP Encrypted Email"
    Set ComBody Of hoEmail To "Sample RSAES-OAEP Encrypted Email"
    Set ComFrom Of hoEmail To "support@chilkatsoft.com"

    // Add a recipient.
    // (The email is encrypted using the recipient's certificate.
    // If sending to multiple recipients, then the AddEncryptCert method would
    // need to be called once for each recipient's certificate.)
    Get ComAddTo Of hoEmail "Chilkat GMail" "chilkat.support@gmail.com" To iSuccess

    // Set the email object properties to indicate the desired encryption.
    Set ComPkcs7CryptAlg Of hoEmail To "aes"
    // If AES-256 is desired, set the following property to 256.
    Set ComPkcs7KeyLength Of hoEmail To 128
    Set ComOaepPadding Of hoEmail To True
    // Other choices for the OAEP hash algorithm are "sha1", "sha384", and "sha512"
    Set ComOaepHash Of hoEmail To "sha256"

    // Provide the certificate to be used for encryption
    Get pvComObject of hoCert to vCert
    Get ComAddEncryptCert Of hoEmail vCert To iSuccess

    // Don't forget to indicate that the email should be encrypted when sent..
    Set ComSendEncrypted Of hoEmail To True

    // The mailman object will do the RSAES-OAEP encryption when sending.
    Get Create (RefClass(cComChilkatMailMan)) To hoMailman
    If (Not(IsComObjectCreated(hoMailman))) Begin
        Send CreateComObject of hoMailman
    End

    // Set the SMTP settings for your email account on your mail server.
    Set ComSmtpUsername Of hoMailman To "SMTP_LOGIN"
    Set ComSmtpPassword Of hoMailman To "SMTP_PASSWORD"
    Set ComSmtpHost Of hoMailman To "MY_SMTP_DOMAIN_OR_IP"
    Set ComSmtpPort Of hoMailman To 587
    Set ComStartTLS Of hoMailman To True

    // Send the email.  The mailman will encrypt the email as directed by the 
    // property settings of the email object.
    Get pvComObject of hoEmail to vEmail
    Get ComSendEmail Of hoMailman vEmail To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComCloseSmtpConnection Of hoMailman To iSuccess
    If (iSuccess <> True) Begin
        Showln "Connection to SMTP server not closed cleanly."
    End

    Showln "Mail Sent!"

    // -----------------------------------------------------------
    // This is an example of an RSAES-OAEP encrypted email
    // -----------------------------------------------------------

    // MIME-Version: 1.0
    // Date: Thu, 27 Apr 2017 08:43:32 -0500
    // Message-ID: <772DC039F0259C474BAC60240EA2BA2272402308@CHILKAT13>
    // Content-Type: application/pkcs7-mime; name="smime.p7m"; smime-type="enveloped-data"
    // Content-Transfer-Encoding: base64
    // X-Priority: 3 (Normal)
    // Subject: Sample RSAES-OAEP Encrypted Email
    // From: support@chilkatsoft.com
    // To: "Chilkat GMail" <chilkat.support@gmail.com>
    // Content-Disposition: attachment; filename="smime.p7m"
    // 
    // MIICWQYJKoZIhvcNAQcDoIICSjCCAkYCAQAxggGgMIIBnAIBADB1MGgxCzAJBgNVBAYTAlVTMQsw
    // CQYDVQQIDAJJTDEQMA4GA1UEBwwHV2hlYXRvbjEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQ
    // dHkgTHRkMRcwFQYDVQQDDA5DaGlsa2F0V2lkZ2V0cwIJAMRwugDmvniwMBwGCSqGSIb3DQEBBzAP
    // oA0wCwYJYIZIAWUDBAIBBIIBABr0E1dKJjK0FAYRz/NCtJmBeB0nUxgcuBnhGPI/UeyGub3cCo+K
    // G5F4/iVTBJqVvYIF5+fvBnyTginwv7OiUiFWLyihiFC3NIyZJO22+XMHpNatCffTPZk10WswkTgk
    // G3ApnRvGQAaldnFD0Hs8drPU4vBvY9QsjT7YDGa6u2NMX+sr1ewEZArqU0mNfJ6RsEYd5FQbFEVF
    // qLmnz8Dt+yhoJlUtfUd8TXIeqHRJ7RxKOTSzlBZaAdTv2QX4oL9IcAgZeTg5iw+yRPkSAwWyg+I/
    // 7fybLsUpRGDHTGUU+AvHvP0kYKa1mkvccBVEC/+4hEyhpS1tWIR5ByY6vM76Z+8wgZwGCSqGSIb3
    // DQEHATAdBglghkgBZQMEAQIEEKqD0YDHX1NsVDaV32UczpeAcLteQyRPTV4hATjwcPiVelPfeWNs
    // xZKRGaEBqLM8+Y+V4ciCFoOlgJuOcP4m1PTHyilfzd+SCsKz5l1C7+sfPf36n2aacX6IWga59Bz5
    // QbWrOHDUT7O5PnGwKVgQFw3Cj4GrdPGWKcoqxB0HuKnj3WA=


End_Procedure