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

Connect to IMAP Through an SSH Tunnel

See more IMAP Examples

Demonstrates the Chilkat Imap.SshOpenTunnel method, which connects to an SSH server and prepares a tunnel through which a later IMAP connection is routed. The first argument is the SSH server hostname and the second is the SSH port (usually 22). After it succeeds, authenticate the tunnel, then call Connect and Login for the IMAP server.

Background: SSH tunneling (port forwarding) routes the IMAP session inside an encrypted SSH connection. It is used to reach a mail server that is only accessible from behind a bastion/jump host, or to add a second layer of transport security. The order is fixed: open the tunnel, authenticate to SSH, then connect and log in to IMAP — and close the tunnel with SshCloseTunnel when finished.

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;

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

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  sshPort: Integer;
  sshPassword: string;

begin
  success := False;

  //  Demonstrates the Imap.SshOpenTunnel method, which connects to an SSH server and prepares a
  //  tunnel for a later IMAP connection.  The 1st argument is the SSH server hostname and the
  //  2nd is the SSH port (usually 22).

  imap := TImap.Create;

  //  Open the SSH tunnel.  Port 22 is the usual SSH port.
  sshPort := 22;
  success := imap.SshOpenTunnel('ssh.example.com',sshPort);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

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

  //  Authenticate the SSH tunnel with a username and password.
  success := imap.SshAuthenticatePw('sshUser',sshPassword);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  With the SSH tunnel in place, connect and log in to the IMAP server as usual.  The IMAP
  //  traffic flows through the tunnel automatically.
  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('user@example.com','myPassword');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

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

  //  Close the tunnel when finished with the IMAP connection.
  success := imap.SshCloseTunnel();
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;


  imap.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.