Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

IMAP SSH Tunneling (Port Forwarding)

Demonstrates how to setup and use an SSH tunnel for IMAP.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL cSshHostname
LOCAL nSshPort
LOCAL nMsgCount
LOCAL nUpperBound
LOCAL oEmail
LOCAL i
LOCAL nBUid

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oImap := CreateObject("Chilkat.Imap")

//  The SSH hostname may be a hostname or an 
//  IP address, such as "192.168.1.108".
//  The port is typically 22 (the standard port for SSH).
cSshHostname := "www.mysshserver.com"
nSshPort := 22

//  Connect to an SSH server and establish the SSH tunnel:
nSuccess := oImap:SshOpenTunnel(cSshHostname, nSshPort)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Authenticate with the SSH server via a login/password
//  or with a public key.
//  This example demonstrates SSH password authentication.
//  Note: This is not authenticating with the IMAP server, it is
//  for authenticating with the SSH server, which is separate.
nSuccess := oImap:SshAuthenticatePw("mySshLogin", "mySshPassword")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  OK, the SSH tunnel is setup.  The IMAP component may
//  be used exactly the same as usual, except all communications
//  are sent through the SSH tunnel.

//  Connect to an IMAP server via the SSH tunnel.
//  Because the SSH tunnel has been previously setup,
//  this does not establish a direct connection with the IMAP
//  server.  It directs the SSH server to establish the connection.

//  In this example, the IMAP server requires SSL/TLS.  The TLS connection
//  will be enclosed within the SSH tunnel.
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.my-imap-server.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Authenticate with the IMAP server via the SSH tunnel.
nSuccess := oImap:Login("myLogin", "myPassword")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  How many messages in Inbox?
nMsgCount := oImap:NumMessages
IF (nMsgCount == 0)
    ? "No messages found."
    oImap:destroy()
    RETURN
ENDIF

nUpperBound := 10
IF (nMsgCount < nUpperBound)
    nUpperBound := nMsgCount
ENDIF

//  Download up to the 1st 10 messages.
oEmail := CreateObject("Chilkat.Email")

nBUid := 0
FOR i := 1 TO nUpperBound

    nSuccess := oImap:FetchEmail(0, i, nBUid, oEmail)
    IF (nSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oEmail:destroy()
        RETURN
    ENDIF

    ? oEmail:From
    ? oEmail:Subject
    ? "----"
NEXT

//  Disconnect from the IMAP server.
//  The SSH tunnel remains open.
nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oEmail:destroy()
    RETURN
ENDIF

//  It is possible to re-use the existing SSH tunnel for the next connection:
nSuccess := oImap:Connect("imap.my-imap-server2.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oEmail:destroy()
    RETURN
ENDIF

//  Review the LastErrorText to see that the connection was made via the SSH tunnel:
? oImap:LastErrorText

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

//  Finally, close the SSH tunnel.
nSuccess := oImap:SshCloseTunnel()
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oEmail:destroy()
    RETURN
ENDIF

? "IMAP SSH tunneling example completed."

oImap:destroy()
oEmail:destroy()