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

Add a PEM Source to MailMan from PEM Text in Memory

See more POP3 Examples

Demonstrates the Chilkat MailMan.AddPemSource method with PEM text that is held in memory. The PEM text is loaded into a Pem object with LoadPem, whose second argument is the password of the private key, and the Pem object is added to the MailMan object's internal list of sources used for locating certificates and private keys. This example loads the PEM text, adds the source, and fetches a message Chilkat can decrypt.

Background: Pem.LoadPem loads PEM text that is already in memory. It is the right choice when the certificate and private key come from somewhere other than a file — a database, a secrets manager, or an HTTPS download — letting you supply them without first writing sensitive key material to disk. (Pem.LoadPemFile loads a PEM file directly.) 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 MailMan.AddPemSource method with PEM text that is held in memory.  The
'  certificates and private keys of the Pem object are added to the MailMan object's internal
'  list of sources used for locating certificates and private keys.

Dim mailman As ChilkatMailMan
mailman.Initialize("mailman")

'  Configure the POP3 server connection.
mailman.MailHost = "pop.example.com"
mailman.MailPort = 995
mailman.PopSsl = True
mailman.PopUsername = "user@example.com"
mailman.PopPassword = "myPassword"

'  The PEM text (a certificate and its private key) is in memory.  Here it is loaded into a
'  StringBuilder from a file; it could come from a database or a secrets store.
Dim sbPem As ChilkatStringBuilder
sbPem.Initialize
success = sbPem.LoadFile("qa_data/certs/decryption.pem", "utf-8")
If success = False Then
    Log(sbPem.LastErrorText)
    Return
End If


'  Load the PEM text into a Pem object.  The 2nd argument 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.LoadPem(sbPem.GetAsString, "pem_password")
If success = False Then
    Log(pem.LastErrorText)
    Return
End If


'  Add the Pem object as a source of certificates and private keys.
success = mailman.AddPemSource(pem)
If success = False Then
    Log(mailman.LastErrorText)
    Return
End If


'  Fetch a message; if it is encrypted, Chilkat can decrypt it using the PEM source.
Dim email As ChilkatEmail
email.Initialize
success = mailman.FetchOne(False, 0, 1, email)
If success = False Then
    Log(mailman.LastErrorText)
    Return
End If


Log("Decrypted = " & email.Decrypted)

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

'  Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
'  connects and authenticates -- using the property settings above -- whenever a server
'  operation requires it.  Calling the explicit connect/authenticate methods can still be
'  helpful to determine whether a failure occurs while connecting or while authenticating.