Android™
Android™
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 Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// 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.
CkImap imap = new CkImap();
// Open and authenticate an SSH tunnel on a Socket object. This tunnel can be shared with
// other Chilkat objects.
CkSocket sshTunnel = new CkSocket();
int sshPort = 22;
success = sshTunnel.SshOpenTunnel("ssh.example.com",sshPort);
if (success == false) {
Log.i(TAG, sshTunnel.lastErrorText());
return;
}
// 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.
String sshPassword;
success = sshTunnel.SshAuthenticatePw("sshUser",sshPassword);
if (success == false) {
Log.i(TAG, sshTunnel.lastErrorText());
return;
}
// Tell the Imap object to use the existing tunnel.
success = imap.UseSshTunnel(sshTunnel);
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
// Now connect and log in to the IMAP server through the tunnel.
imap.put_Ssl(true);
imap.put_Port(993);
success = imap.Connect("imap.example.com");
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
success = imap.Login("user@example.com","myPassword");
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
success = imap.Disconnect();
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}