Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
IMAP Download and Verify Signed (S/MIME) Email
See more IMAP Examples
Demonstrates how to download and verify digitally signed S/MIME email.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.Imap,
Chilkat.Cert,
Chilkat.Email,
Chilkat.MessageSet;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imap: TImap;
fetchUids: Boolean;
messageSet: TMessageSet;
email: TEmail;
cert: TCert;
i: Integer;
uid: string;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imap := TImap.Create;
// Connect to an IMAP server.
// Use TLS
imap.Ssl := True;
imap.Port := 993;
success := imap.Connect('imap.example.com');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
success := imap.Login('myLogin','myPassword');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Select an IMAP mailbox
success := imap.SelectMailbox('Inbox');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// We can choose to fetch UIDs or sequence numbers.
fetchUids := True;
// Get the message IDs of all the emails in the mailbox
messageSet := TMessageSet.Create;
success := imap.QueryMbx('ALL',fetchUids,messageSet);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
email := TEmail.Create;
cert := TCert.Create;
i := 0;
while i < messageSet.Count do
begin
uid := messageSet.GetId(i);
WriteLn('uid: ' + uid);
success := imap.FetchEmail(False,uid,True,email);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// The security layers of signed and/or encrypted emails
// are automatically "unwrapped" when loaded into
// a Chilkat email object.
// An application only needs to check to see if an email
// was received signed or encrypted, and then examine
// the success/failure. For example:
if (email.ReceivedSigned = True) then
begin
WriteLn('This email was signed.');
// Check to see if the signatures were verified.
if (email.SignaturesValid = True) then
begin
WriteLn('Digital signature(s) verified.');
WriteLn('Signer: ' + email.SignedBy);
// Get the certificate used for signing.
success := email.LastSignerCert(0,cert);
if (success = False) then
begin
WriteLn('Failed to get signing certificate object.');
end
else
begin
WriteLn('Signing cert: ' + cert.SubjectCN);
end;
end
else
begin
WriteLn('Digital signature verification failed.');
end;
end;
i := i + 1;
end;
// Disconnect from the IMAP server.
success := imap.Disconnect();
imap.Free;
messageSet.Free;
email.Free;
cert.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.