Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Share an Existing SSH Tunnel (Socket) with IMAP
See more IMAP Examples
Demonstrates the Chilkat Imap.UseSshTunnel method, which uses an existing SSH tunnel, represented by a Socket object, for IMAP connections. The only argument is the Socket. Call it before Connect. This example opens and authenticates the tunnel on a Socket, then routes IMAP through it.
Background: A
Socket object can open and hold an SSH tunnel that several Chilkat objects share. This is the approach when the tunnel is managed independently of any one protocol object — for example a long-lived tunnel reused across IMAP, POP3, and SMTP sessions — whereas SshOpenTunnel ties the tunnel's lifetime to the Imap object.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.Socket;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imap: TImap;
sshTunnel: TSocket;
sshPort: Integer;
sshPassword: string;
begin
success := False;
// Demonstrates the Imap.UseSshTunnel method, which uses an existing SSH tunnel (represented by
// a Socket) for IMAP connections. The only argument is the Socket. Call it before Connect.
imap := TImap.Create;
// Open and authenticate an SSH tunnel on a Socket object. This tunnel can be shared with
// other Chilkat objects.
sshTunnel := TSocket.Create;
sshPort := 22;
success := sshTunnel.SshOpenTunnel('ssh.example.com',sshPort);
if (success = False) then
begin
WriteLn(sshTunnel.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.
success := sshTunnel.SshAuthenticatePw('sshUser',sshPassword);
if (success = False) then
begin
WriteLn(sshTunnel.LastErrorText);
Exit;
end;
// Tell the Imap object to use the existing tunnel.
success := imap.UseSshTunnel(sshTunnel);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Now connect and log in to the IMAP server through the tunnel.
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;
imap.Free;
sshTunnel.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.