Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Set the Decryption Certificate and Private Key
See more POP3 Examples
Demonstrates the Chilkat MailMan.SetDecryptCert2 method, which explicitly specifies both the certificate and its associated private key, as separate objects, to use when decrypting S/MIME encrypted email. This example loads a .cer certificate and a PEM private key, registers both, and fetches a message Chilkat can decrypt.
Background: Credentials are not always packaged together. When the certificate is a standalone
.cer and the private key is a separate PEM (or other) file, SetDecryptCert2 takes the two objects individually. It is the two-object counterpart to SetDecryptCert, which expects a single certificate that already carries its private key (such as one loaded from a PFX).Chilkat Pascal (Lazarus/Delphi) 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.PrivateKey,
Chilkat.Cert,
Chilkat.Email;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
mailman: TMailMan;
cert: TCert;
privKey: TPrivateKey;
email: TEmail;
begin
success := False;
// Demonstrates the MailMan.SetDecryptCert2 method, which explicitly specifies both the
// certificate and its associated private key (as separate objects) to use when decrypting
// S/MIME encrypted email.
mailman := TMailMan.Create;
// Configure the POP3 server connection.
mailman.MailHost := 'pop.example.com';
mailman.MailPort := 995;
mailman.PopSsl := True;
mailman.PopUsername := 'user@example.com';
mailman.PopPassword := 'myPassword';
// Load the certificate (public) and the matching private key from separate files.
cert := TCert.Create;
success := cert.LoadFromFile('qa_data/certs/recipient.cer');
if (success = False) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
privKey := TPrivateKey.Create;
success := privKey.LoadPemFile('qa_data/certs/recipient_privkey.pem');
if (success = False) then
begin
WriteLn(privKey.LastErrorText);
Exit;
end;
// Specify the certificate and private key to use when decrypting downloaded email.
success := mailman.SetDecryptCert2(cert,privKey);
if (success = False) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
// Fetch a message; if it is encrypted, Chilkat decrypts it using the certificate and key.
email := TEmail.Create;
success := mailman.FetchOne(False,0,1,email);
if (success = False) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
WriteLn('Decrypted = ' + email.Decrypted);
// Note: Paths such as "qa_data/..." are relative local filesystem paths,
// relative to the current working directory of the running application.
// Note: Explicitly connecting/authenticating is optional. Chilkat MailMan automatically
// connects and authenticates -- using the property settings above -- whenever a server
// operation requires it. Calling the explicit connect/authenticate methods can still be
// helpful to determine whether a failure occurs while connecting or while authenticating.
mailman.Free;
cert.Free;
privKey.Free;
email.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.