Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
IMAP Read Encrypted Email
See more IMAP Examples
Demonstrates how to read encrypted email from an IMAP mailbox.Reading encrypted email works the same as reading non-encrypted email. If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.
Information about the original encrypted state of the email is available after it has been downloaded and decrypted.
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.MessageSet,
Chilkat.Imap,
Chilkat.Cert,
Chilkat.Email,
Chilkat.JsonObject,
Chilkat.Secrets;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imap: TImap;
secrets: TSecrets;
json: TJsonObject;
password: string;
messageSet: TMessageSet;
uid: LongWord;
email: TEmail;
cert: TCert;
begin
success := False;
imap := TImap.Create;
imap.Ssl := True;
imap.Port := 993;
success := imap.Connect('imap.example2.com');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// We'll get the IMAP email account's password from the Apple Keychain or Windows Credentials Manager.
// See how we originally saved the email credentials to the Keychain here:
// Save Email Credentials in Apple Keychain or Windows Credentials Manager
secrets := TSecrets.Create;
// On Windows, this is the Windows Credentials Manager
// On MacOS/iOS, it is the Apple Keychain
secrets.Location := 'local_manager';
// Specify the name of the secret.
// service and username are required.
// appName and domain are optional.
// Note: The values are arbitrary and can be anything you want.
json := TJsonObject.Create;
json.UpdateString('appName','MyEmailApp');
json.UpdateString('service','IMAP');
json.UpdateString('domain','example2.com');
json.UpdateString('username','jane@example2.com');
password := secrets.GetSecretStr(json);
if (secrets.LastMethodSuccess = False) then
begin
WriteLn(secrets.LastErrorText);
Exit;
end;
success := imap.Login('jane@example2.com',password);
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;
// This example: Send Encrypted Email using Certificate in Apple Keychain
// sent an email with the subject "This email is encrypted".
// Let's download an email with the word "encrypted" in the subject.
messageSet := TMessageSet.Create;
success := imap.QueryMbx('SUBJECT encrypted',True,messageSet);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
if (messageSet.Count = 0) then
begin
WriteLn('No messages found.');
Exit;
end;
// Reading encrypted email works the same as reading non-encrypted email.
// If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store),
// Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.
uid := messageSet.GetId(0);
email := TEmail.Create;
success := imap.FetchEmail(False,uid,True,email);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Here we can show if the email was received encrypted, if it was successfully decrypted, and
// which certificate was used to decrypt.
WriteLn('Email received encrypted: ' + email.ReceivedEncrypted);
// Was it successfully decrypted?
WriteLn('Successfully decrypted: ' + email.Decrypted);
// What cert was used to decrypt?
WriteLn('Encrypted by: ' + email.EncryptedBy);
cert := TCert.Create;
success := email.LastDecryptCert(cert);
if (success <> False) then
begin
WriteLn('Certificate DN: ' + cert.SubjectDN);
end;
// Show the decrypted email body.
WriteLn(email.Body);
imap.Disconnect();
imap.Free;
secrets.Free;
json.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.