Unicode C++
Unicode C++
Using Client Certificate w/ IMAP SSL
Demonstrates how to use a client-side certificate with an IMAP SSL connection. The SetSslClientCert method is called to specify a certificate to be used for the SSL connection.Chilkat Unicode C++ Downloads
#include <CkImapW.h>
#include <CkCertStoreW.h>
#include <CkJsonObjectW.h>
#include <CkCertW.h>
#include <CkMessageSetW.h>
#include <CkEmailBundleW.h>
#include <CkEmailW.h>
void ChilkatSample(void)
{
bool success = false;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkImapW imap;
// To use a secure SSL connection, set SSL and the port:
imap.put_Ssl(true);
// The typical port for IMAP SSL is 993
imap.put_Port(993);
// Load a certificate from a PFX file and use it.
// Note: Other methods are available to load pre-installed
// certificates from registry-based certificate stores.
// Create an instance of a certificate store object, load a PFX file,
// locate the certificate we need, and use it for signing.
// (a PFX file may contain more than one certificate.)
CkCertStoreW certStore;
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
success = certStore.LoadPfxFile(L"myCertWithPrivateKey.pfx",L"secret");
if (success == false) {
wprintf(L"%s\n",certStore.lastErrorText());
return;
}
// Find the certificate by the subject common name:
CkJsonObjectW jsonCN;
jsonCN.UpdateString(L"CN",L"cert common name");
CkCertW cert;
success = certStore.FindCert(jsonCN,cert);
if (success == false) {
wprintf(L"%s\n",certStore.lastErrorText());
return;
}
// If a PFX file is known to contain a single certificate,
// you may load it directly into a Chilkat certificate object.
// This snippet of source code shows how:
CkCertW cert2;
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
success = cert2.LoadPfxFile(L"myClientCert.pfx",L"secret");
if (success == false) {
wprintf(L"%s\n",cert.lastErrorText());
return;
}
// Use the cert:
success = imap.SetSslClientCert(cert);
// Connect to an IMAP server.
success = imap.Connect(L"imap.example.com");
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
// Login
success = imap.Login(L"myLogin",L"myPassword");
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
// Select an IMAP mailbox
success = imap.SelectMailbox(L"Inbox");
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
// Get the message IDs of all the emails in the mailbox
bool fetchUids = true;
CkMessageSetW messageSet;
success = imap.QueryMbx(L"ALL",fetchUids,messageSet);
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
// Fetch the emails into a bundle object:
CkEmailBundleW bundle;
bool headersOnly = false;
success = imap.FetchMsgSet(headersOnly,messageSet,bundle);
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
// Loop over the bundle and display the FROM and SUBJECT of each.
CkEmailW email;
int i = 0;
int numEmails = bundle.get_MessageCount();
while (i < numEmails) {
bundle.EmailAt(i,email);
wprintf(L"%s\n",email.ck_from());
wprintf(L"%s\n",email.subject());
wprintf(L"--\n");
i = i + 1;
}
// Disconnect from the IMAP server.
success = imap.Disconnect();
}