Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
oleobject loo_SshTunnel
integer li_SshPort
string ls_SshPassword

li_Success = 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.

loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Open and authenticate an SSH tunnel on a Socket object.  This tunnel can be shared with
//  other Chilkat objects.
loo_SshTunnel = create oleobject
li_rc = loo_SshTunnel.ConnectToNewObject("Chilkat.Socket")

li_SshPort = 22
li_Success = loo_SshTunnel.SshOpenTunnel("ssh.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_SshTunnel.LastErrorText
    destroy loo_Imap
    destroy loo_SshTunnel
    return
end if

//  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.

li_Success = loo_SshTunnel.SshAuthenticatePw("sshUser",ls_SshPassword)
if li_Success = 0 then
    Write-Debug loo_SshTunnel.LastErrorText
    destroy loo_Imap
    destroy loo_SshTunnel
    return
end if

//  Tell the Imap object to use the existing tunnel.
li_Success = loo_Imap.UseSshTunnel(loo_SshTunnel)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_SshTunnel
    return
end if

//  Now connect and log in to the IMAP server through the tunnel.
loo_Imap.Ssl = 1
loo_Imap.Port = 993
li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_SshTunnel
    return
end if

li_Success = loo_Imap.Login("user@example.com","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_SshTunnel
    return
end if

li_Success = loo_Imap.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_SshTunnel
    return
end if



destroy loo_Imap
destroy loo_SshTunnel