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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Email
oleobject loo_Signer
oleobject loo_Recipient
oleobject loo_Mail
oleobject loo_Rendered

li_Success = 0

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

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
    destroy loo_Email
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Email.LoadEml("with-attachments.eml")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    return
end if

loo_Signer = create oleobject
li_rc = loo_Signer.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Signer.LoadPfxFile("signer.pfx","SIGNER_PASSWORD")
if li_Success <> 1 then
    Write-Debug loo_Signer.LastErrorText
    destroy loo_Email
    destroy loo_Signer
    return
end if

loo_Recipient = create oleobject
li_rc = loo_Recipient.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Recipient.LoadFromFile("recipient.cer")
if li_Success <> 1 then
    Write-Debug loo_Recipient.LastErrorText
    destroy loo_Email
    destroy loo_Signer
    destroy loo_Recipient
    return
end if

li_Success = loo_Email.SetSigningCert(loo_Signer)
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Signer
    destroy loo_Recipient
    return
end if

li_Success = loo_Email.SetEncryptCert(loo_Recipient)
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Signer
    destroy loo_Recipient
    return
end if

loo_Email.SigningHashAlg = "sha256"
loo_Email.Pkcs7CryptAlg = "aes"
loo_Email.Pkcs7KeyLength = 256
loo_Email.OaepPadding = 1
loo_Email.OaepHash = "sha256"
loo_Email.OaepMgfHash = "sha256"
loo_Email.SendSigned = 1
loo_Email.SendEncrypted = 1
loo_Mail = create oleobject
li_rc = loo_Mail.ConnectToNewObject("Chilkat.MailMan")

loo_Rendered = create oleobject
li_rc = loo_Rendered.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Mail.RenderToMimeBd(loo_Email,loo_Rendered)
if li_Success <> 1 then
    Write-Debug loo_Mail.LastErrorText
    destroy loo_Email
    destroy loo_Signer
    destroy loo_Recipient
    destroy loo_Mail
    destroy loo_Rendered
    return
end if

li_Success = loo_Rendered.WriteFile("signed-encrypted.eml")
if li_Success <> 1 then
    Write-Debug "Failed: rendered.WriteFile."
    destroy loo_Email
    destroy loo_Signer
    destroy loo_Recipient
    destroy loo_Mail
    destroy loo_Rendered
    return
end if

Write-Debug "Saved signed-encrypted.eml."


destroy loo_Email
destroy loo_Signer
destroy loo_Recipient
destroy loo_Mail
destroy loo_Rendered