B4X Requires Chilkat v11.8.0+
B4X
Add a PEM Source (PEM Text in Memory) for S/MIME Decryption
See more IMAP Examples
Demonstrates the Chilkat Imap.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 as a source of certificates and private keys for S/MIME processing. Call AddPemSource once for each additional source.
Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.
Background: When downloading encrypted (S/MIME) email, Chilkat needs the recipient's private key to decrypt it. Adding one or more PEM sources gives Chilkat a pool of keys to search; the matching key is used automatically as messages are fetched.
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.) On Windows and macOS the system certificate stores are also searched automatically. AddPemSource was added in Chilkat v11.8.0 and replaces UseCertVault and the AddPfxSource methods.Chilkat B4X Downloads
Dim success As Boolean = False
' Demonstrates the Imap.AddPemSource method with PEM text that is held in memory. The
' certificates and private keys of the Pem object are added as a source of certificates and
' private keys for S/MIME decryption. The only argument is the Pem object.
Dim imap As ChilkatImap
imap.Initialize("imap")
imap.Ssl = True
imap.Port = 993
success = imap.Connect("imap.example.com")
If success = False Then
Log(imap.LastErrorText)
Return
End If
success = imap.Login("user@example.com", "myPassword")
If success = False Then
Log(imap.LastErrorText)
Return
End If
' The PEM text (a certificate and its private key) is in memory. Here it is loaded into a
' StringBuilder from a file.
Dim sbPem As ChilkatStringBuilder
sbPem.Initialize
success = sbPem.LoadFile("qa_data/smime.pem", "utf-8")
If success = False Then
Log(sbPem.LastErrorText)
Return
End If
' The password of the private key is not hard-coded in source. Insert code here to obtain it
' from an interactive prompt, environment variable, or a secrets vault.
Dim pemPassword As String
' Load the PEM text into a Pem object. The 2nd argument of LoadPem is the password of the private key.
Dim pem As ChilkatPem
pem.Initialize
success = pem.LoadPem(sbPem.GetAsString, pemPassword)
If success = False Then
Log(pem.LastErrorText)
Return
End If
' Add the Pem object as a source for S/MIME certificates and private keys. Call once per source.
success = imap.AddPemSource(pem)
If success = False Then
Log(imap.LastErrorText)
Return
End If
' Messages downloaded after this are automatically decrypted when a matching key is found.
success = imap.Disconnect
If success = False Then
Log(imap.LastErrorText)
Return
End If