PowerBuilder
PowerBuilder
Set the Signing Certificate and Private Key
See more Email Object Examples
Demonstrates the Chilkat Email.SetSigningCert2 method, which explicitly sets the certificate and its corresponding private key as separate objects for sending digitally signed email. This example loads a .cer certificate and a PEM private key, sets both, and enables signed sending.
Background: When your signing certificate and its private key are stored separately — a public
.cer plus a PEM key file — rather than combined in a PFX, SetSigningCert2 accepts the two objects individually. It is the two-object counterpart to SetSigningCert, which takes a single certificate that already carries its private key.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Email
oleobject loo_Cert
oleobject loo_PrivKey
li_Success = 0
// Demonstrates the SetSigningCert2 method, which explicitly sets the certificate and its
// corresponding private key (as separate objects) for sending digitally signed email.
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
loo_Email.Subject = "Signed email"
loo_Email.Body = "This message will be sent with a digital signature."
loo_Email.From = "alice@example.com"
loo_Email.AddTo("Bob","bob@example.com")
// Load the certificate (public) and the matching private key from separate files.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
li_Success = loo_Cert.LoadFromFile("qa_data/certs/signer.cer")
if li_Success = 0 then
Write-Debug loo_Cert.LastErrorText
destroy loo_Email
destroy loo_Cert
return
end if
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")
li_Success = loo_PrivKey.LoadPemFile("qa_data/certs/signer_privkey.pem")
if li_Success = 0 then
Write-Debug loo_PrivKey.LastErrorText
destroy loo_Email
destroy loo_Cert
destroy loo_PrivKey
return
end if
// Set the certificate and private key used to create the signature.
li_Success = loo_Email.SetSigningCert2(loo_Cert,loo_PrivKey)
if li_Success = 0 then
Write-Debug loo_Email.LastErrorText
destroy loo_Email
destroy loo_Cert
destroy loo_PrivKey
return
end if
loo_Email.SendSigned = 1
Write-Debug "Signing certificate and private key set."
// Note: Paths such as "qa_data/..." are relative local filesystem paths,
// relative to the current working directory of the running application.
destroy loo_Email
destroy loo_Cert
destroy loo_PrivKey