Sample code for 30+ languages & platforms
B4X Requires Chilkat v11.8.0+

Add a PEM Source of Certificates and Private Keys to an Email

See more Email Object Examples

Demonstrates the Chilkat Email.AddPemSource method, which adds the certificates and private keys of a Pem object to the email's internal certificate and private-key lookup sources for S/MIME decryption and signature verification. This example loads a PEM file into a Pem object and gives it to the email.

Background: A Pem object is an in-memory collection of certificates and private keys. It can be loaded from PEM text or a PEM file, from a .p7b, or filled one certificate at a time. Instead of wiring up each certificate individually for every operation, you give the Pem object to the email; Chilkat then draws on its certificates automatically whenever it needs a key — to decrypt an incoming message or to verify a signature. This is especially convenient on platforms without an OS certificate store. AddPemSource can be called multiple times. Each call adds more certificates to the ones already added. AddPemSource was added in Chilkat v11.8.0 and replaces UseCertVault and the AddPfxSource methods.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

'  Demonstrates the AddPemSource method, which adds the certificates and private keys of a
'  Pem object to the email's internal certificate and private-key lookup sources for
'  S/MIME decryption and signature verification.

Dim email As ChilkatEmail
email.Initialize

'  Load a PEM file (certificate + private key) into a Pem object.  The 2nd argument of LoadPemFile
'  is the password of the private key.  (Pass an empty string if the private key is not encrypted.)
Dim pem As ChilkatPem
pem.Initialize
success = pem.LoadPemFile("qa_data/certs/certs.pem", "pem_password")
If success = False Then
    Log(pem.LastErrorText)
    Return
End If


'  Make the certificates and private keys available to the email object.
'  AddPemSource can be called multiple times.  Each call adds more certificates to the ones already added.
success = email.AddPemSource(pem)
If success = False Then
    Log(email.LastErrorText)
    Return
End If


Log("Certificates and private keys added to the email's lookup sources.")

'  Note: An encrypted email is decrypted when it is loaded.  Call AddPemSource before loading
'  the email.

'  Note: The path "qa_data/certs/certs.pem" is a relative local filesystem path,
'  relative to the current working directory of the running application.