Unicode C
Unicode C
Add a PFX Source to MailMan from BinData
See more POP3 Examples
Demonstrates the Chilkat MailMan.AddPfxSourceBd method, which adds a PFX/PKCS#12 certificate store to the MailMan object's internal list of sources used for locating certificates and private keys. The first argument is a BinData containing the PFX bytes and the second is the PFX password. This example loads a PFX into a BinData, registers it, and fetches a message Chilkat can decrypt.
Background: This is the in-memory (
BinData) counterpart to AddPfxSourceFile. It is the right choice when the PFX bytes come from somewhere other than a file — a database, a secrets manager, or an HTTPS download — letting you supply the certificate and private key without first writing sensitive key material to disk.Chilkat Unicode C Downloads
#include <C_CkMailManW.h>
#include <C_CkBinDataW.h>
#include <C_CkEmailW.h>
void ChilkatSample(void)
{
BOOL success;
HCkMailManW mailman;
HCkBinDataW bdPfx;
HCkEmailW email;
success = FALSE;
// Demonstrates the MailMan.AddPfxSourceBd method, which adds a PFX/PKCS#12 certificate store
// to the MailMan object's internal list of sources used for locating certificates and
// private keys. The 1st argument is a BinData containing the PFX bytes and the 2nd is the
// PFX password.
mailman = CkMailManW_Create();
// Configure the POP3 server connection.
CkMailManW_putMailHost(mailman,L"pop.example.com");
CkMailManW_putMailPort(mailman,995);
CkMailManW_putPopSsl(mailman,TRUE);
CkMailManW_putPopUsername(mailman,L"user@example.com");
CkMailManW_putPopPassword(mailman,L"myPassword");
// Load the PFX bytes into a BinData (they could come from a file, database, or secrets store).
bdPfx = CkBinDataW_Create();
success = CkBinDataW_LoadFile(bdPfx,L"qa_data/certs/decryption.pfx");
if (success == FALSE) {
wprintf(L"%s\n",CkBinDataW_lastErrorText(bdPfx));
CkMailManW_Dispose(mailman);
CkBinDataW_Dispose(bdPfx);
return;
}
// Add the PFX as a source of certificates and private keys.
success = CkMailManW_AddPfxSourceBd(mailman,bdPfx,L"pfx_password");
if (success == FALSE) {
wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
CkMailManW_Dispose(mailman);
CkBinDataW_Dispose(bdPfx);
return;
}
// Fetch a message; if it is encrypted, Chilkat can decrypt it using the PFX source.
email = CkEmailW_Create();
success = CkMailManW_FetchOne(mailman,FALSE,0,1,email);
if (success == FALSE) {
wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
CkMailManW_Dispose(mailman);
CkBinDataW_Dispose(bdPfx);
CkEmailW_Dispose(email);
return;
}
wprintf(L"Decrypted = %d\n",CkEmailW_getDecrypted(email));
// Note: The path "qa_data/certs/decryption.pfx" is a relative local filesystem path,
// relative to the current working directory of the running application.
// Note: Explicitly connecting/authenticating is optional. Chilkat MailMan automatically
// connects and authenticates -- using the property settings above -- whenever a server
// operation requires it. Calling the explicit connect/authenticate methods can still be
// helpful to determine whether a failure occurs while connecting or while authenticating.
CkMailManW_Dispose(mailman);
CkBinDataW_Dispose(bdPfx);
CkEmailW_Dispose(email);
}