C++ Requires Chilkat v11.0.0+
C++
Sign and encrypt an email with S/MIME
See more Email Object Examples
Use the sender’s signing PFX and the recipient’s public RSA certificate with with-attachments.eml, which has one recipient. Supply your own signing-key password from a credential store. Render to bytes to apply both S/MIME operations without connecting to SMTP.
Chilkat C++ Downloads
#include <CkEmail.h>
#include <CkCert.h>
#include <CkMailMan.h>
#include <CkBinData.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkEmail email;
success = email.LoadEml("with-attachments.eml");
if (success != true) {
std::cout << email.lastErrorText() << "\r\n";
return;
}
CkCert signer;
success = signer.LoadPfxFile("signer.pfx","SIGNER_PASSWORD");
if (success != true) {
std::cout << signer.lastErrorText() << "\r\n";
return;
}
CkCert recipient;
success = recipient.LoadFromFile("recipient.cer");
if (success != true) {
std::cout << recipient.lastErrorText() << "\r\n";
return;
}
success = email.SetSigningCert(signer);
if (success != true) {
std::cout << email.lastErrorText() << "\r\n";
return;
}
success = email.SetEncryptCert(recipient);
if (success != true) {
std::cout << email.lastErrorText() << "\r\n";
return;
}
email.put_SigningHashAlg("sha256");
email.put_Pkcs7CryptAlg("aes");
email.put_Pkcs7KeyLength(256);
email.put_OaepPadding(true);
email.put_OaepHash("sha256");
email.put_OaepMgfHash("sha256");
email.put_SendSigned(true);
email.put_SendEncrypted(true);
CkMailMan mail;
CkBinData rendered;
success = mail.RenderToMimeBd(email,rendered);
if (success != true) {
std::cout << mail.lastErrorText() << "\r\n";
return;
}
success = rendered.WriteFile("signed-encrypted.eml");
if (success != true) {
std::cout << "Failed: rendered.WriteFile." << "\r\n";
return;
}
std::cout << "Saved signed-encrypted.eml." << "\r\n";
}