Sample code for 30+ languages & platforms
Delphi ActiveX 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 ActiveX Downloads

Delphi ActiveX
var
success: Integer;
email: TChilkatEmail;
signer: TChilkatCert;
recipient: TChilkatCert;
mail: TChilkatMailMan;
rendered: TChilkatBinData;

begin
success := 0;

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

email := TChilkatEmail.Create(Self);
success := email.LoadEml('with-attachments.eml');
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
signer := TChilkatCert.Create(Self);
success := signer.LoadPfxFile('signer.pfx','SIGNER_PASSWORD');
if (success <> 1) then
  begin
    Memo1.Lines.Add(signer.LastErrorText);
    Exit;
  end;
recipient := TChilkatCert.Create(Self);
success := recipient.LoadFromFile('recipient.cer');
if (success <> 1) then
  begin
    Memo1.Lines.Add(recipient.LastErrorText);
    Exit;
  end;
success := email.SetSigningCert(signer.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
success := email.SetEncryptCert(recipient.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
email.SigningHashAlg := 'sha256';
email.Pkcs7CryptAlg := 'aes';
email.Pkcs7KeyLength := 256;
email.OaepPadding := 1;
email.OaepHash := 'sha256';
email.OaepMgfHash := 'sha256';
email.SendSigned := 1;
email.SendEncrypted := 1;
mail := TChilkatMailMan.Create(Self);
rendered := TChilkatBinData.Create(Self);
success := mail.RenderToMimeBd(email.ControlInterface,rendered.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(mail.LastErrorText);
    Exit;
  end;
success := rendered.WriteFile('signed-encrypted.eml');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Failed: rendered.WriteFile.');
    Exit;
  end;
Memo1.Lines.Add('Saved signed-encrypted.eml.');