Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
email: HCkEmail;
signer: HCkCert;
recipient: HCkCert;
mail: HCkMailMan;
rendered: HCkBinData;

begin
success := False;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

email := CkEmail_Create();
success := CkEmail_LoadEml(email,'with-attachments.eml');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
signer := CkCert_Create();
success := CkCert_LoadPfxFile(signer,'signer.pfx','SIGNER_PASSWORD');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkCert__lastErrorText(signer));
    Exit;
  end;
recipient := CkCert_Create();
success := CkCert_LoadFromFile(recipient,'recipient.cer');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkCert__lastErrorText(recipient));
    Exit;
  end;
success := CkEmail_SetSigningCert(email,signer);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
success := CkEmail_SetEncryptCert(email,recipient);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
CkEmail_putSigningHashAlg(email,'sha256');
CkEmail_putPkcs7CryptAlg(email,'aes');
CkEmail_putPkcs7KeyLength(email,256);
CkEmail_putOaepPadding(email,True);
CkEmail_putOaepHash(email,'sha256');
CkEmail_putOaepMgfHash(email,'sha256');
CkEmail_putSendSigned(email,True);
CkEmail_putSendEncrypted(email,True);
mail := CkMailMan_Create();
rendered := CkBinData_Create();
success := CkMailMan_RenderToMimeBd(mail,email,rendered);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mail));
    Exit;
  end;
success := CkBinData_WriteFile(rendered,'signed-encrypted.eml');
if (success <> True) then
  begin
    Memo1.Lines.Add('Failed: rendered.WriteFile.');
    Exit;
  end;
Memo1.Lines.Add('Saved signed-encrypted.eml.');

CkEmail_Dispose(email);
CkCert_Dispose(signer);
CkCert_Dispose(recipient);
CkMailMan_Dispose(mail);
CkBinData_Dispose(rendered);