Unicode C++ Requires Chilkat v11.8.0+
Unicode C++
Add a PEM Source of Certificates and Private Keys for IMAP
See more IMAP Examples
Demonstrates the Chilkat Imap.AddPemSource method, which adds the certificates and private keys of a Pem object to the Imap object's sources of certificates and private keys for S/MIME operations. The only argument is the Pem object.
Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.
Background: A
Pem object is an in-memory collection of certificates and private keys. It can be loaded from PEM text, from a .p7b, or, as in this example, from a PEM file. It is a convenient way to give an application the exact set of credentials it needs without depending on the operating system's certificate stores. Adding it makes all of its certificates available for S/MIME decryption and signature verification. 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 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 to the Imap object's sources of certificates and private keys for S/MIME
// operations. 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;
}
success = imap.Disconnect();
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
}