Sample code for 30+ languages & platforms
Visual FoxPro

S/MIME Encrypt .eml without Sending

See more Email Object Examples

Demonstrates how to encrypt an email using the recipient's digital certificate. This example just encrypts, and does not send the email.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loEmail
LOCAL loCert
LOCAL loSbSmime
LOCAL loMailman

lnSuccess = 0

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

loEmail = CreateObject('Chilkat.Email')

lnSuccess = loEmail.LoadEml("c:/temp/email/unencrypted.eml")
IF (lnSuccess = 0) THEN
    ? loEmail.LastErrorText
    RELEASE loEmail
    CANCEL
ENDIF

* The email content is encrypted using AES with a 256-bit key, operating in GCM mode, which provides authenticated encryption.
loEmail.Pkcs7CryptAlg = "aes-gcm"
loEmail.Pkcs7KeyLength = 256
loEmail.OaepPadding = 1
loEmail.OaepHash = "sha256"
loEmail.OaepMgfHash = "sha256"

loCert = CreateObject('Chilkat.Cert')
lnSuccess = loCert.LoadFromFile("c/temps/cert/recipient.cer")
IF (lnSuccess = 0) THEN
    ? loCert.LastErrorText
    RELEASE loEmail
    RELEASE loCert
    CANCEL
ENDIF

loEmail.SendEncrypted = 1
loEmail.SetEncryptCert(loCert)

loSbSmime = CreateObject('Chilkat.StringBuilder')

* The mailman object applies the encryption by rendering the email according to the instructions (property settings) provided in the email object.
* No email is sent.
loMailman = CreateObject('Chilkat.MailMan')
lnSuccess = loMailman.RenderToMimeSb(loEmail,loSbSmime)
IF (lnSuccess = 0) THEN
    ? loMailman.LastErrorText
    RELEASE loEmail
    RELEASE loCert
    RELEASE loSbSmime
    RELEASE loMailman
    CANCEL
ENDIF

lnSuccess = loSbSmime.WriteFile("c:/temp/encryptedEmail.eml","utf-8",0)
IF (lnSuccess = 0) THEN
    ? loMailman.LastErrorText
    RELEASE loEmail
    RELEASE loCert
    RELEASE loSbSmime
    RELEASE loMailman
    CANCEL
ENDIF

? "Success!"

RELEASE loEmail
RELEASE loCert
RELEASE loSbSmime
RELEASE loMailman