Unicode C++ Requires Chilkat v11.8.0+
Unicode C++
Add a PEM Source for S/MIME Decryption
See more IMAP Examples
Demonstrates the Chilkat Imap.AddPemSource method, which adds the certificates and private keys of a Pem object as a source of certificates and private keys for S/MIME processing. This example loads them from a PEM file. 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: Registering the private keys needed to decrypt S/MIME mail lets Chilkat decrypt messages transparently during fetch. Add several sources if recipients' keys are spread across multiple PEM files. On Windows and macOS the system stores (and Keychain) are searched automatically in addition to the sources you add. A PEM file is a text file that can hold a certificate, the certificates of its chain, and the private key, each between
-----BEGIN ...----- and -----END ...----- lines. The private key can be encrypted with a password. AddPemSource was added in Chilkat v11.8.0 and replaces UseCertVault and the AddPfxSource methods.Chilkat Unicode C++ Downloads
#include <CkImapW.h>
#include <CkPemW.h>
void ChilkatSample(void)
{
bool success = false;
// Demonstrates the Imap.AddPemSource method, which adds the certificates and private keys of a
// Pem object 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 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 a PEM file that has the certificate and its private key. The 2nd argument of LoadPemFile
// is the password of the private key.
CkPemW pem;
success = pem.LoadPemFile(L"qa_data/smime.pem",pemPassword);
if (success == false) {
wprintf(L"%s\n",pem.lastErrorText());
return;
}
// Add the certificates and private keys to the Imap object.
// AddPemSource can be called multiple times. Each call adds more certificates to the ones already added.
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;
}
}