Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oSsh
LOCAL nSshPort
LOCAL cSshPassword

nSuccess := 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.

oImap := CreateObject("Chilkat.Imap")

//  Establish and authenticate the SSH connection separately.
oSsh := CreateObject("Chilkat.Ssh")
nSshPort := 22
nSuccess := oSsh:Connect("ssh.example.com", nSshPort)
IF (nSuccess == 0)
    ? oSsh:LastErrorText
    oImap:destroy()
    oSsh: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 := oSsh:AuthenticatePw("sshUser", cSshPassword)
IF (nSuccess == 0)
    ? oSsh:LastErrorText
    oImap:destroy()
    oSsh:destroy()
    RETURN
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.
nSuccess := oImap:UseSsh(oSsh)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oSsh:destroy()
    RETURN
ENDIF

//  Now connect and log in to the IMAP server through the SSH connection.
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oSsh:destroy()
    RETURN
ENDIF

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

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

oImap:destroy()
oSsh:destroy()