Sample code for 30+ languages & platforms
Visual FoxPro

Send an Email S/MIME Encrypted

See more Email Object Examples

Demonstrates the Chilkat Email.SendEncrypted property. Set it to true to have the email sent S/MIME encrypted (the default is false). Setting this property alone is not enough — encryption requires one or more recipient certificates, supplied with AddEncryptCert or SetEncryptCert. This example loads a recipient certificate, registers it, and enables encrypted sending.

Background: To encrypt a message for someone, you need their public certificate — which is why only a .cer file (no private key) is required here. The message is scrambled so that only the holder of the matching private key, i.e. the intended recipient, can read it. When there are multiple recipients, you add each one's certificate, and Chilkat encrypts the one-time content key separately for each so they can all decrypt the same message.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loEmail
LOCAL loCert

lnSuccess = 0

*  Demonstrates the Email.SendEncrypted property.  Set to true to have the email sent
*  S/MIME encrypted.  Encryption also requires one or more recipient certificates,
*  supplied via AddEncryptCert or SetEncryptCert.  The default is false.

loEmail = CreateObject('Chilkat.Email')
loEmail.Subject = "Encrypted email"
loEmail.Body = "This message will be sent S/MIME encrypted."
loEmail.From = "alice@example.com"
loEmail.AddTo("Bob","bob@example.com")

*  Load the recipient's certificate (only the public key is needed to encrypt).
loCert = CreateObject('Chilkat.Cert')
lnSuccess = loCert.LoadFromFile("qa_data/certs/recipient.cer")
IF (lnSuccess = 0) THEN
    ? loCert.LastErrorText
    RELEASE loEmail
    RELEASE loCert
    CANCEL
ENDIF

*  Supply the recipient certificate used for encryption.
lnSuccess = loEmail.AddEncryptCert(loCert)
IF (lnSuccess = 0) THEN
    ? loEmail.LastErrorText
    RELEASE loEmail
    RELEASE loCert
    CANCEL
ENDIF

*  Request that the email be sent encrypted.
loEmail.SendEncrypted = 1

? "SendEncrypted = " + STR(loEmail.SendEncrypted)

*  Note: Paths such as "qa_data/..." are relative local filesystem paths,
*  relative to the current working directory of the running application.

RELEASE loEmail
RELEASE loCert