Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Use an Existing Socket SSH Tunnel
See more SMTP Examples
Demonstrates the Chilkat MailMan.UseSshTunnel method, which configures MailMan to use an existing SSH tunnel provided by a Socket object for its SMTP and POP3 connections. This example opens and authenticates a tunnel on a Socket, hands it to MailMan, and then verifies an SMTP login through it.
Background: Sharing one already-established SSH tunnel across objects avoids opening (and authenticating) a separate SSH connection for every component that needs to reach the remote network. Here the tunnel lives on a
Socket object, which MailMan then routes its SMTP/POP3 traffic through. It is the Socket-based sibling of UseSsh, which shares an Ssh object instead.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.MailMan,
Chilkat.Socket;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
sshTunnel: TSocket;
mailman: TMailMan;
begin
success := False;
// Demonstrates the MailMan.UseSshTunnel method, which configures MailMan to use an existing
// SSH tunnel provided by a Socket object for its SMTP and POP3 connections.
// Open and authenticate the SSH tunnel using a Socket object.
sshTunnel := TSocket.Create;
success := sshTunnel.SshOpenTunnel('ssh.example.com',22);
if (success = False) then
begin
WriteLn(sshTunnel.LastErrorText);
Exit;
end;
success := sshTunnel.SshAuthenticatePw('sshUser','sshPassword');
if (success = False) then
begin
WriteLn(sshTunnel.LastErrorText);
Exit;
end;
mailman := TMailMan.Create;
// Configure the SMTP server connection. These connections will travel through the tunnel.
mailman.SmtpHost := 'smtp.example.com';
mailman.SmtpPort := 465;
mailman.SmtpSsl := True;
mailman.SmtpUsername := 'user@example.com';
mailman.SmtpPassword := 'myPassword';
// Tell MailMan to route its connections through the existing tunnel.
success := mailman.UseSshTunnel(sshTunnel);
if (success = False) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
// SMTP operations now travel through the shared SSH tunnel.
success := mailman.VerifySmtpLogin();
if (success = False) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
WriteLn('Authenticated with the SMTP server through the SSH tunnel.');
sshTunnel.Free;
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.