Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

;  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 = ObjCreate("Chilkat.Imap")

Local $iSshPort = 22
$bSuccess = $oImap.SshOpenTunnel("ssh.example.com",$iSshPort)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
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.
Local $sshPassword

$bSuccess = $oImap.SshAuthenticatePw("sshUser",$sshPassword)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
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 = True
$oImap.Port = 993
$bSuccess = $oImap.Connect("imap.example.com")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oImap.Login("user@example.com","myPassword")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

;  ... use the IMAP connection ...

$bSuccess = $oImap.Disconnect()
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

;  Close the SSH tunnel.  Do not use the IMAP connection after this point.
$bSuccess = $oImap.SshCloseTunnel()
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf