Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.MailMan,
Chilkat.BinData,
Chilkat.Cert,
Chilkat.Email;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
email: TEmail;
signer: TCert;
recipient: TCert;
mail: TMailMan;
rendered: TBinData;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
email := TEmail.Create;
success := email.LoadEml('with-attachments.eml');
if (success <> True) then
begin
WriteLn(email.LastErrorText);
Exit;
end;
signer := TCert.Create;
success := signer.LoadPfxFile('signer.pfx','SIGNER_PASSWORD');
if (success <> True) then
begin
WriteLn(signer.LastErrorText);
Exit;
end;
recipient := TCert.Create;
success := recipient.LoadFromFile('recipient.cer');
if (success <> True) then
begin
WriteLn(recipient.LastErrorText);
Exit;
end;
success := email.SetSigningCert(signer);
if (success <> True) then
begin
WriteLn(email.LastErrorText);
Exit;
end;
success := email.SetEncryptCert(recipient);
if (success <> True) then
begin
WriteLn(email.LastErrorText);
Exit;
end;
email.SigningHashAlg := 'sha256';
email.Pkcs7CryptAlg := 'aes';
email.Pkcs7KeyLength := 256;
email.OaepPadding := True;
email.OaepHash := 'sha256';
email.OaepMgfHash := 'sha256';
email.SendSigned := True;
email.SendEncrypted := True;
mail := TMailMan.Create;
rendered := TBinData.Create;
success := mail.RenderToMimeBd(email,rendered);
if (success <> True) then
begin
WriteLn(mail.LastErrorText);
Exit;
end;
success := rendered.WriteFile('signed-encrypted.eml');
if (success <> True) then
begin
WriteLn('Failed: rendered.WriteFile.');
Exit;
end;
WriteLn('Saved signed-encrypted.eml.');
email.Free;
signer.Free;
recipient.Free;
mail.Free;
rendered.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.