Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

IMAP Login using SecureString Credentials

See more IMAP Examples

Demonstrates the Chilkat Imap.LoginSecure method, which authenticates a connected session using SecureString credentials. The first argument is the login name and the second is the password. This is the secure-string counterpart of Login; the mechanism is chosen by the AuthMethod property.

Background: A SecureString keeps the credential encrypted in memory and helps avoid leaving plaintext copies lying around in the process, which is why the password should come from an interactive prompt, an environment variable, or a secrets vault — never a hard-coded literal. For XOAUTH2, pass the login name in the first argument and the raw access token (without a Bearer prefix) in the second.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.SecureString;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  password: string;
  secureLogin: TSecureString;
  securePassword: TSecureString;

begin
  success := False;

  //  Demonstrates the Imap.LoginSecure method, which authenticates the session using SecureString
  //  credentials.  The 1st argument is the login name and the 2nd is the password.

  imap := TImap.Create;

  imap.Ssl := True;
  imap.Port := 993;

  success := imap.Connect('imap.example.com');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  The password is not hard-coded in source.  Insert code here to obtain it from an
  //  interactive prompt, environment variable, or a secrets vault.

  //  Place the login name and password into SecureString objects.
  secureLogin := TSecureString.Create;
  secureLogin.Append('user@example.com');
  securePassword := TSecureString.Create;
  securePassword.Append(password);

  success := imap.LoginSecure(secureLogin,securePassword);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  WriteLn('Logged in securely.');

  success := imap.Disconnect();
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;


  imap.Free;
  secureLogin.Free;
  securePassword.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.