Sample code for 30+ languages & platforms
Visual FoxPro

Share an Existing SSH Tunnel (Socket) with IMAP

See more IMAP Examples

Demonstrates the Chilkat Imap.UseSshTunnel method, which uses an existing SSH tunnel, represented by a Socket object, for IMAP connections. The only argument is the Socket. Call it before Connect. This example opens and authenticates the tunnel on a Socket, then routes IMAP through it.

Background: A Socket object can open and hold an SSH tunnel that several Chilkat objects share. This is the approach when the tunnel is managed independently of any one protocol object — for example a long-lived tunnel reused across IMAP, POP3, and SMTP sessions — whereas SshOpenTunnel ties the tunnel's lifetime to the Imap object.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImap
LOCAL loSshTunnel
LOCAL lnSshPort
LOCAL lcSshPassword

lnSuccess = 0

*  Demonstrates the Imap.UseSshTunnel method, which uses an existing SSH tunnel (represented by
*  a Socket) for IMAP connections.  The only argument is the Socket.  Call it before Connect.

loImap = CreateObject('Chilkat.Imap')

*  Open and authenticate an SSH tunnel on a Socket object.  This tunnel can be shared with
*  other Chilkat objects.
loSshTunnel = CreateObject('Chilkat.Socket')
lnSshPort = 22
lnSuccess = loSshTunnel.SshOpenTunnel("ssh.example.com",lnSshPort)
IF (lnSuccess = 0) THEN
    ? loSshTunnel.LastErrorText
    RELEASE loImap
    RELEASE loSshTunnel
    CANCEL
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.

lnSuccess = loSshTunnel.SshAuthenticatePw("sshUser",lcSshPassword)
IF (lnSuccess = 0) THEN
    ? loSshTunnel.LastErrorText
    RELEASE loImap
    RELEASE loSshTunnel
    CANCEL
ENDIF

*  Tell the Imap object to use the existing tunnel.
lnSuccess = loImap.UseSshTunnel(loSshTunnel)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loSshTunnel
    CANCEL
ENDIF

*  Now connect and log in to the IMAP server through the tunnel.
loImap.Ssl = 1
loImap.Port = 993
lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loSshTunnel
    CANCEL
ENDIF

lnSuccess = loImap.Login("user@example.com","myPassword")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loSshTunnel
    CANCEL
ENDIF

lnSuccess = loImap.Disconnect()
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loSshTunnel
    CANCEL
ENDIF

RELEASE loImap
RELEASE loSshTunnel