Sample code for 30+ languages & platforms
DataFlex

Open an SSH Tunnel for POP3

See more POP3 Examples

Demonstrates the Chilkat MailMan.SshOpenTunnel method, which connects to an SSH server and opens an SSH tunnel for MailMan's SMTP or POP3 connections. The first argument is the SSH server's hostname or IP address and the second is the SSH port. This example opens a tunnel, authenticates, performs a POP3 operation through it, and closes the tunnel.

Background: An SSH tunnel (port forwarding) wraps another protocol's traffic inside an encrypted SSH connection. Here, MailMan's POP3 or SMTP session is carried through the SSH server rather than connecting directly. This is useful when a mail server is only reachable from inside a network you can reach via SSH, or when policy requires all traffic to traverse a bastion host. Opening the tunnel is step one; you must then authenticate to the SSH server before the tunnel carries traffic.

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.SshOpenTunnel method, which connects to an SSH server and opens
    //  an SSH tunnel for MailMan's SMTP or POP3 connections.  The 1st argument is the SSH server
    //  hostname or IP address, and the 2nd is the SSH port.

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

    //  Configure the POP3 server connection.  These connections will travel through the tunnel.
    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 the SSH tunnel (port 22 is the standard SSH port).
    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

    //  Authenticate with the SSH server.
    Get ComSshAuthenticatePw Of hoMailman "sshUser" "sshPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  POP3 operations now travel through the SSH 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 tunnel when finished.
    Get ComSshCloseTunnel Of hoMailman To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure