Sample code for 30+ languages & platforms
Delphi DLL

S/MIME Encrypt .eml without Sending

See more Email Object Examples

Demonstrates how to encrypt an email using the recipient's digital certificate. This example just encrypts, and does not send the email.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, MailMan, StringBuilder, Cert, Email;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
email: HCkEmail;
cert: HCkCert;
sbSmime: HCkStringBuilder;
mailman: HCkMailMan;

begin
success := False;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

email := CkEmail_Create();

success := CkEmail_LoadEml(email,'c:/temp/email/unencrypted.eml');
if (success = False) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;

// The email content is encrypted using AES with a 256-bit key, operating in GCM mode, which provides authenticated encryption.
CkEmail_putPkcs7CryptAlg(email,'aes-gcm');
CkEmail_putPkcs7KeyLength(email,256);
CkEmail_putOaepPadding(email,True);
CkEmail_putOaepHash(email,'sha256');
CkEmail_putOaepMgfHash(email,'sha256');

cert := CkCert_Create();
success := CkCert_LoadFromFile(cert,'c/temps/cert/recipient.cer');
if (success = False) then
  begin
    Memo1.Lines.Add(CkCert__lastErrorText(cert));
    Exit;
  end;

CkEmail_putSendEncrypted(email,True);
CkEmail_SetEncryptCert(email,cert);

sbSmime := CkStringBuilder_Create();

// The mailman object applies the encryption by rendering the email according to the instructions (property settings) provided in the email object.
// No email is sent.
mailman := CkMailMan_Create();
success := CkMailMan_RenderToMimeSb(mailman,email,sbSmime);
if (success = False) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

success := CkStringBuilder_WriteFile(sbSmime,'c:/temp/encryptedEmail.eml','utf-8',False);
if (success = False) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

Memo1.Lines.Add('Success!');

CkEmail_Dispose(email);
CkCert_Dispose(cert);
CkStringBuilder_Dispose(sbSmime);
CkMailMan_Dispose(mailman);

end;