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

Visual FoxPro
LOCAL lnSuccess
LOCAL loEmail
LOCAL loSigner
LOCAL loRecipient
LOCAL loMail
LOCAL loRendered

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loEmail = CreateObject('Chilkat.Email')
lnSuccess = loEmail.LoadEml("with-attachments.eml")
IF (lnSuccess <> 1) THEN
    ? loEmail.LastErrorText
    RELEASE loEmail
    CANCEL
ENDIF

loSigner = CreateObject('Chilkat.Cert')
lnSuccess = loSigner.LoadPfxFile("signer.pfx","SIGNER_PASSWORD")
IF (lnSuccess <> 1) THEN
    ? loSigner.LastErrorText
    RELEASE loEmail
    RELEASE loSigner
    CANCEL
ENDIF

loRecipient = CreateObject('Chilkat.Cert')
lnSuccess = loRecipient.LoadFromFile("recipient.cer")
IF (lnSuccess <> 1) THEN
    ? loRecipient.LastErrorText
    RELEASE loEmail
    RELEASE loSigner
    RELEASE loRecipient
    CANCEL
ENDIF

lnSuccess = loEmail.SetSigningCert(loSigner)
IF (lnSuccess <> 1) THEN
    ? loEmail.LastErrorText
    RELEASE loEmail
    RELEASE loSigner
    RELEASE loRecipient
    CANCEL
ENDIF

lnSuccess = loEmail.SetEncryptCert(loRecipient)
IF (lnSuccess <> 1) THEN
    ? loEmail.LastErrorText
    RELEASE loEmail
    RELEASE loSigner
    RELEASE loRecipient
    CANCEL
ENDIF

loEmail.SigningHashAlg = "sha256"
loEmail.Pkcs7CryptAlg = "aes"
loEmail.Pkcs7KeyLength = 256
loEmail.OaepPadding = 1
loEmail.OaepHash = "sha256"
loEmail.OaepMgfHash = "sha256"
loEmail.SendSigned = 1
loEmail.SendEncrypted = 1
loMail = CreateObject('Chilkat.MailMan')
loRendered = CreateObject('Chilkat.BinData')
lnSuccess = loMail.RenderToMimeBd(loEmail,loRendered)
IF (lnSuccess <> 1) THEN
    ? loMail.LastErrorText
    RELEASE loEmail
    RELEASE loSigner
    RELEASE loRecipient
    RELEASE loMail
    RELEASE loRendered
    CANCEL
ENDIF

lnSuccess = loRendered.WriteFile("signed-encrypted.eml")
IF (lnSuccess <> 1) THEN
    ? "Failed: rendered.WriteFile."
    RELEASE loEmail
    RELEASE loSigner
    RELEASE loRecipient
    RELEASE loMail
    RELEASE loRendered
    CANCEL
ENDIF

? "Saved signed-encrypted.eml."

RELEASE loEmail
RELEASE loSigner
RELEASE loRecipient
RELEASE loMail
RELEASE loRendered