C
C
Set the Signing Certificate for an Email
See more Email Object Examples
Demonstrates the Chilkat Email.SetSigningCert method, which sets the certificate (with access to its private key) used to create a digital signature when sending signed email. This example loads the signing certificate from a PFX, sets it, and enables signed sending.
Background: Signing uses your own private key (the opposite of encrypting, which uses the recipient's public key), so the signing certificate must include private-key access. A PFX (PKCS#12) file bundles the certificate and its private key together, which is why it is loaded here with
LoadPfxFile. If the certificate and key are in separate files, use SetSigningCert2 instead.Chilkat C Downloads
#include <C_CkEmail.h>
#include <C_CkCert.h>
void ChilkatSample(void)
{
BOOL success;
HCkEmail email;
HCkCert cert;
success = FALSE;
// Demonstrates the SetSigningCert method, which sets the certificate (with access to its
// private key) used for creating a digital signature when sending signed email.
email = CkEmail_Create();
CkEmail_putSubject(email,"Signed email");
CkEmail_putBody(email,"This message will be sent with a digital signature.");
CkEmail_putFrom(email,"alice@example.com");
CkEmail_AddTo(email,"Bob","bob@example.com");
// Load a signing certificate together with its private key from a PFX file.
cert = CkCert_Create();
success = CkCert_LoadPfxFile(cert,"qa_data/certs/signer.pfx","pfx_password");
if (success == FALSE) {
printf("%s\n",CkCert_lastErrorText(cert));
CkEmail_Dispose(email);
CkCert_Dispose(cert);
return;
}
// Set the certificate used to create the signature.
success = CkEmail_SetSigningCert(email,cert);
if (success == FALSE) {
printf("%s\n",CkEmail_lastErrorText(email));
CkEmail_Dispose(email);
CkCert_Dispose(cert);
return;
}
// Request that the email be sent with a digital signature.
CkEmail_putSendSigned(email,TRUE);
printf("Signing certificate set.\n");
// Note: The path "qa_data/certs/signer.pfx" is a relative local filesystem path,
// relative to the current working directory of the running application.
CkEmail_Dispose(email);
CkCert_Dispose(cert);
}