Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
mailman: HCkMailMan;
count: Integer;

begin
success := False;

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

mailman := CkMailMan_Create();

//  Configure the POP3 server connection.
CkMailMan_putMailHost(mailman,'pop.example.com');
CkMailMan_putMailPort(mailman,995);
CkMailMan_putPopSsl(mailman,True);
CkMailMan_putPopUsername(mailman,'user@example.com');
CkMailMan_putPopPassword(mailman,'myPassword');

//  Open and authenticate the SSH tunnel.
success := CkMailMan_SshOpenTunnel(mailman,'ssh.example.com',22);
if (success = False) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

success := CkMailMan_SshAuthenticatePw(mailman,'sshUser','sshPassword');
if (success = False) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

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

//  Close the SSH tunnel when finished with it.
success := CkMailMan_SshCloseTunnel(mailman);
if (success = False) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

Memo1.Lines.Add('SSH tunnel closed.');

CkMailMan_Dispose(mailman);