Xbase++ Requires Chilkat v11.0.0+
Xbase++
POP3 SSH Tunneling (Port Forwarding)
Demonstrates how to connect to a POP3 server through an SSH tunnel. Reads a POP3 mailbox and display the FROM and SUBJECT header fields of each email.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oMailman
LOCAL cSshHostname
LOCAL nSshPort
LOCAL nNumToDownload
LOCAL i
LOCAL oEmail
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The mailman object is used for receiving (POP3)
// and sending (SMTP) email.
oMailman := CreateObject("Chilkat.MailMan")
// 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 := "sftp.example.com"
nSshPort := 22
// Connect to an SSH server and establish the SSH tunnel:
nSuccess := oMailman:SshOpenTunnel(cSshHostname, nSshPort)
IF (nSuccess == 0)
? oMailman:LastErrorText
oMailman: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 POP3 server, it is
// for authenticating with the SSH server, which is separate.
nSuccess := oMailman:SshAuthenticatePw("ssh_login", "ssh_password")
IF (nSuccess == 0)
? oMailman:LastErrorText
oMailman:destroy()
RETURN
ENDIF
// OK, the SSH tunnel is setup. The mailman may
// be used exactly the same as usual, except all communications
// are now sent through the SSH tunnel.
// The SSH tunnel may be kept open for any number of
// POP3 connections.
// Set the POP3 server's hostname
oMailman:MailHost := "mail.my-pop3-server.com"
// Set the POP3 settings for your POP3 server:
oMailman:PopUsername := "pop3_login"
oMailman:PopPassword := "pop3_password"
oMailman:MailPort := 995
oMailman:PopSsl := 1
// Connect to the POP3 server through the already-established SSH tunnel:
nSuccess := oMailman:Pop3BeginSession()
IF (nSuccess == 0)
? oMailman:LastErrorText
oMailman:destroy()
RETURN
ENDIF
// A visual inspection of the LastErrorText after
// a successful POP3 connection will confirm the SSH tunneling.
// Note: If the POP3 connection uses SSL/TLS, then the SSL/TLS
// secure channel will be wrapped within the SSH tunnel.
? oMailman:LastErrorText
// How many messages are in the POP3 inbox?
nNumToDownload := oMailman:GetMailboxCount()
IF (nNumToDownload > 5)
nNumToDownload := 5
ENDIF
IF (nNumToDownload == 0)
? "No messages in the POP3 inbox."
oMailman:destroy()
RETURN
ENDIF
// Download a max of 5 messages
oEmail := CreateObject("Chilkat.Email")
FOR i := 1 TO nNumToDownload
nSuccess := oMailman:FetchOne(0, 0, i, oEmail)
IF (nSuccess == 0)
? oMailman:LastErrorText
oMailman:destroy()
oEmail:destroy()
RETURN
ENDIF
? oEmail:From
? oEmail:Subject
? "----"
NEXT
// Close the connection with the POP3 server, leaving the SSH tunnel open for subsequent POP3 connections.
nSuccess := oMailman:Pop3EndSession()
IF (nSuccess == 0)
? oMailman:LastErrorText
oMailman:destroy()
oEmail:destroy()
RETURN
ENDIF
// Try connecting to a different POP3 server through the same aleady setup SSH tunnel:
// Set the POP3 server's hostname
oMailman:MailHost := "mail.my-pop3-server2.com"
// Set the POP3 settings for your POP3 server:
oMailman:PopUsername := "pop3_login2"
oMailman:PopPassword := "pop3_password2"
oMailman:MailPort := 110
oMailman:PopSsl := 0
nSuccess := oMailman:Pop3BeginSession()
IF (nSuccess == 0)
? oMailman:LastErrorText
oMailman:destroy()
oEmail:destroy()
RETURN
ENDIF
// Review the LastErrorText to see that the connection was made via the SSH tunnel:
? oMailman:LastErrorText
nSuccess := oMailman:Pop3EndSession()
IF (nSuccess == 0)
? oMailman:LastErrorText
oMailman:destroy()
oEmail:destroy()
RETURN
ENDIF
// Close the SSH tunnel.
nSuccess := oMailman:SshCloseTunnel()
IF (nSuccess == 0)
? oMailman:LastErrorText
oMailman:destroy()
oEmail:destroy()
RETURN
ENDIF
oMailman:destroy()
oEmail:destroy()