Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkImap.pb"
IncludeFile "CkSocket.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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.

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Open and authenticate an SSH tunnel on a Socket object.  This tunnel can be shared with
    ;  other Chilkat objects.
    sshTunnel.i = CkSocket::ckCreate()
    If sshTunnel.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sshPort.i = 22
    success = CkSocket::ckSshOpenTunnel(sshTunnel,"ssh.example.com",sshPort)
    If success = 0
        Debug CkSocket::ckLastErrorText(sshTunnel)
        CkImap::ckDispose(imap)
        CkSocket::ckDispose(sshTunnel)
        ProcedureReturn
    EndIf

    ;  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.
    sshPassword.s

    success = CkSocket::ckSshAuthenticatePw(sshTunnel,"sshUser",sshPassword)
    If success = 0
        Debug CkSocket::ckLastErrorText(sshTunnel)
        CkImap::ckDispose(imap)
        CkSocket::ckDispose(sshTunnel)
        ProcedureReturn
    EndIf

    ;  Tell the Imap object to use the existing tunnel.
    success = CkImap::ckUseSshTunnel(imap,sshTunnel)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkSocket::ckDispose(sshTunnel)
        ProcedureReturn
    EndIf

    ;  Now connect and log in to the IMAP server through the tunnel.
    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkSocket::ckDispose(sshTunnel)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"user@example.com","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkSocket::ckDispose(sshTunnel)
        ProcedureReturn
    EndIf

    success = CkImap::ckDisconnect(imap)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkSocket::ckDispose(sshTunnel)
        ProcedureReturn
    EndIf



    CkImap::ckDispose(imap)
    CkSocket::ckDispose(sshTunnel)


    ProcedureReturn
EndProcedure