Unicode C++ Requires Chilkat v11.8.0+
Unicode C++
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 Unicode C++ Downloads
#include <CkImapW.h>
#include <CkStringBuilderW.h>
#include <CkPemW.h>
void ChilkatSample(void)
{
bool success = 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.
CkImapW imap;
imap.put_Ssl(true);
imap.put_Port(993);
success = imap.Connect(L"imap.example.com");
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
success = imap.Login(L"user@example.com",L"myPassword");
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
// The PEM text (a certificate and its private key) is in memory. Here it is loaded into a
// StringBuilder from a file.
CkStringBuilderW sbPem;
success = sbPem.LoadFile(L"qa_data/smime.pem",L"utf-8");
if (success == false) {
wprintf(L"%s\n",sbPem.lastErrorText());
return;
}
// 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.
const wchar_t *pemPassword = 0;
// Load the PEM text into a Pem object. The 2nd argument of LoadPem is the password of the private key.
CkPemW pem;
success = pem.LoadPem(sbPem.getAsString(),pemPassword);
if (success == false) {
wprintf(L"%s\n",pem.lastErrorText());
return;
}
// 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) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
// Messages downloaded after this are automatically decrypted when a matching key is found.
success = imap.Disconnect();
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
}