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

Close the SSH Tunnel

See more POP3 Examples

Demonstrates the Chilkat MailMan.SshCloseTunnel method, which closes the SSH tunnel used for SMTP or POP3 communication. This example opens and authenticates a tunnel, performs a mail operation through it, and then closes it.

Background: An SSH tunnel holds an open connection to the SSH server for as long as it is needed, so closing it releases both that connection and the resources on the SSH server. The natural pattern is open → authenticate → do mail work → close. It is the counterpart to SshOpenTunnel, and is separate from closing the mail connections themselves (CloseSmtpConnection / Pop3EndSession), which ride inside the tunnel.

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.MailMan;

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

procedure RunDemo;
var
  success: Boolean;
  mailman: TMailMan;
  count: Integer;

begin
  success := False;

  //  Demonstrates the MailMan.SshCloseTunnel method, which closes the SSH tunnel used for SMTP
  //  or POP3 communication.

  mailman := TMailMan.Create;

  //  Configure the POP3 server connection.
  mailman.MailHost := 'pop.example.com';
  mailman.MailPort := 995;
  mailman.PopSsl := True;
  mailman.PopUsername := 'user@example.com';
  mailman.PopPassword := 'myPassword';

  //  Open and authenticate the SSH tunnel.
  success := mailman.SshOpenTunnel('ssh.example.com',22);
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  success := mailman.SshAuthenticatePw('sshUser','sshPassword');
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  //  ... perform mail operations through the tunnel ...
  count := mailman.GetMailboxCount();
  if (count < 0) then
    begin
      WriteLn('Failed to get the mailbox count.');
    end
  else
    begin
      WriteLn('Mailbox count: ' + count);
    end;

  //  Close the SSH tunnel when finished with it.
  success := mailman.SshCloseTunnel();
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  WriteLn('SSH tunnel closed.');


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