Sample code for 30+ languages & platforms
DataFlex

Share an Existing SSH Tunnel (Socket) with IMAP

See more IMAP Examples

Demonstrates the Chilkat Imap.UseSshTunnel method, which uses an existing SSH tunnel, represented by a Socket object, for IMAP connections. The only argument is the Socket. Call it before Connect. This example opens and authenticates the tunnel on a Socket, then routes IMAP through it.

Background: A Socket object can open and hold an SSH tunnel that several Chilkat objects share. This is the approach when the tunnel is managed independently of any one protocol object — for example a long-lived tunnel reused across IMAP, POP3, and SMTP sessions — whereas SshOpenTunnel ties the tunnel's lifetime to the Imap object.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Variant vSshTunnel
    Handle hoSshTunnel
    Integer iSshPort
    String sSshPassword
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.UseSshTunnel method, which uses an existing SSH tunnel (represented by
    //  a Socket) for IMAP connections.  The only argument is the Socket.  Call it before Connect.

    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    //  Open and authenticate an SSH tunnel on a Socket object.  This tunnel can be shared with
    //  other Chilkat objects.
    Get Create (RefClass(cComChilkatSocket)) To hoSshTunnel
    If (Not(IsComObjectCreated(hoSshTunnel))) Begin
        Send CreateComObject of hoSshTunnel
    End
    Move 22 To iSshPort
    Get ComSshOpenTunnel Of hoSshTunnel "ssh.example.com" iSshPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSshTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  The SSH password is not hard-coded in source.  Insert code here to obtain it from an
    //  interactive prompt, environment variable, or a secrets vault.

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

    //  Tell the Imap object to use the existing tunnel.
    Get pvComObject of hoSshTunnel to vSshTunnel
    Get ComUseSshTunnel Of hoImap vSshTunnel To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Now connect and log in to the IMAP server through the tunnel.
    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993
    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLogin Of hoImap "user@example.com" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure