PureBasic
PureBasic
Route IMAP Through an Existing Ssh Connection
See more IMAP Examples
Demonstrates the Chilkat Imap.UseSsh method, which uses an already connected and authenticated Ssh object as the transport for IMAP connections. The only argument is the Ssh object. Call it before Connect. This example establishes the SSH connection separately, then routes IMAP through it.
Background: SSH supports multiple logical channels over one connection, so a single authenticated
Ssh object can be shared — IMAP, SMTP, and other Chilkat objects can all tunnel through the same SSH session. This differs from SshOpenTunnel, where the Imap object owns and manages the tunnel itself; with UseSsh you manage the Ssh object's lifetime.Chilkat PureBasic Downloads
IncludeFile "CkSsh.pb"
IncludeFile "CkImap.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the Imap.UseSsh method, which uses an already connected and authenticated Ssh
; object as the transport for IMAP connections. The only argument is the Ssh object. Call it
; before Connect.
imap.i = CkImap::ckCreate()
If imap.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Establish and authenticate the SSH connection separately.
ssh.i = CkSsh::ckCreate()
If ssh.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
sshPort.i = 22
success = CkSsh::ckConnect(ssh,"ssh.example.com",sshPort)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkImap::ckDispose(imap)
CkSsh::ckDispose(ssh)
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 = CkSsh::ckAuthenticatePw(ssh,"sshUser",sshPassword)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkImap::ckDispose(imap)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
; Use the SSH connection as the transport for IMAP. One SSH connection can be shared by
; several Chilkat objects because SSH supports multiple channels.
success = CkImap::ckUseSsh(imap,ssh)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
; Now connect and log in to the IMAP server through the SSH connection.
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)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
success = CkImap::ckLogin(imap,"user@example.com","myPassword")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
success = CkImap::ckDisconnect(imap)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
CkImap::ckDispose(imap)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndProcedure