Sample code for 30+ languages & platforms
Xbase++

Close an IMAP SSH Tunnel

See more IMAP Examples

Demonstrates the Chilkat Imap.SshCloseTunnel method, which closes the SSH tunnel opened by SshOpenTunnel. It takes no arguments. Any IMAP connection that was using the tunnel must not be used after the tunnel is closed.

Background: The SSH tunnel is the transport that carries the IMAP session, so closing it tears down that transport. Disconnect or finish with the IMAP connection first, then close the tunnel to release the SSH resources cleanly. Continuing to use an IMAP connection whose tunnel has been closed will fail.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nSshPort
LOCAL cSshPassword

nSuccess := 0

//  Demonstrates the Imap.SshCloseTunnel method, which closes the SSH tunnel opened by
//  SshOpenTunnel.  It takes no arguments.  Any IMAP connection using the tunnel must not be
//  used after the tunnel is closed.

oImap := CreateObject("Chilkat.Imap")

nSshPort := 22
nSuccess := oImap:SshOpenTunnel("ssh.example.com", nSshPort)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
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.

nSuccess := oImap:SshAuthenticatePw("sshUser", cSshPassword)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  With the SSH tunnel in place, connect and log in to the IMAP server as usual.  The IMAP
//  traffic flows through the tunnel automatically.
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:Login("user@example.com", "myPassword")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  ... use the IMAP connection ...

nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Close the SSH tunnel.  Do not use the IMAP connection after this point.
nSuccess := oImap:SshCloseTunnel()
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

oImap:destroy()