Sample code for 30+ languages & platforms
Delphi ActiveX

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 Delphi ActiveX Downloads

Delphi ActiveX
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
imap: TChilkatImap;
secrets: TChilkatSecrets;
json: TChilkatJsonObject;
password: WideString;
messageSet: TMessageSet;
uid: Cardinal;
email: TChilkatEmail;
cert: TChilkatCert;

begin
success := 0;

imap := TChilkatImap.Create(Self);

imap.Ssl := 1;
imap.Port := 993;
success := imap.Connect('imap.example2.com');
if (success = 0) then
  begin
    Memo1.Lines.Add(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 := TChilkatSecrets.Create(Self);

// 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 := TChilkatJsonObject.Create(Self);
json.UpdateString('appName','MyEmailApp');
json.UpdateString('service','IMAP');
json.UpdateString('domain','example2.com');
json.UpdateString('username','jane@example2.com');

password := secrets.GetSecretStr(json.ControlInterface);
if (secrets.LastMethodSuccess = 0) then
  begin
    Memo1.Lines.Add(secrets.LastErrorText);
    Exit;
  end;

success := imap.Login('jane@example2.com',password);
if (success = 0) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;

// Select an IMAP mailbox
success := imap.SelectMailbox('Inbox');
if (success = 0) then
  begin
    Memo1.Lines.Add(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(Self);
success := imap.QueryMbx('SUBJECT encrypted',1,messageSet.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;

if (messageSet.Count = 0) then
  begin
    Memo1.Lines.Add('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 := TChilkatEmail.Create(Self);
success := imap.FetchEmail(0,uid,1,email.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(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.
Memo1.Lines.Add('Email received encrypted: ' + IntToStr(Ord(email.ReceivedEncrypted)));

// Was it successfully decrypted?
Memo1.Lines.Add('Successfully decrypted: ' + IntToStr(Ord(email.Decrypted)));

// What cert was used to decrypt?
Memo1.Lines.Add('Encrypted by: ' + email.EncryptedBy);
cert := TChilkatCert.Create(Self);
success := email.LastDecryptCert(cert.ControlInterface);
if (success <> 0) then
  begin
    Memo1.Lines.Add('Certificate DN: ' + cert.SubjectDN);
  end;

// Show the decrypted email body.
Memo1.Lines.Add(email.Body);

imap.Disconnect();
end;