Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMailman
    Integer iCount
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatMailMan)) To hoMailman
    If (Not(IsComObjectCreated(hoMailman))) Begin
        Send CreateComObject of hoMailman
    End

    //  Configure the POP3 server connection.
    Set ComMailHost Of hoMailman To "pop.example.com"
    Set ComMailPort Of hoMailman To 995
    Set ComPopSsl Of hoMailman To True
    Set ComPopUsername Of hoMailman To "user@example.com"
    Set ComPopPassword Of hoMailman To "myPassword"

    //  Open and authenticate the SSH tunnel.
    Get ComSshOpenTunnel Of hoMailman "ssh.example.com" 22 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSshAuthenticatePw Of hoMailman "sshUser" "sshPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  ... perform mail operations through the tunnel ...
    Get ComGetMailboxCount Of hoMailman To iCount
    If (iCount < 0) Begin
        Showln "Failed to get the mailbox count."
    End
    Else Begin
        Showln "Mailbox count: " iCount
    End

    //  Close the SSH tunnel when finished with it.
    Get ComSshCloseTunnel Of hoMailman To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "SSH tunnel closed."


End_Procedure