Sample code for 30+ languages & platforms
Android™

Route IMAP Through an Existing Ssh Connection

See more IMAP Examples

Demonstrates the Chilkat Imap.UseSsh method, which uses an already connected and authenticated Ssh object as the transport for IMAP connections. The only argument is the Ssh object. Call it before Connect. This example establishes the SSH connection separately, then routes IMAP through it.

Background: SSH supports multiple logical channels over one connection, so a single authenticated Ssh object can be shared — IMAP, SMTP, and other Chilkat objects can all tunnel through the same SSH session. This differs from SshOpenTunnel, where the Imap object owns and manages the tunnel itself; with UseSsh you manage the Ssh object's lifetime.

Chilkat Android™ Downloads

Android™
// 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.UseSsh method, which uses an already connected and authenticated Ssh
    //  object as the transport for IMAP connections.  The only argument is the Ssh object.  Call it
    //  before Connect.

    CkImap imap = new CkImap();

    //  Establish and authenticate the SSH connection separately.
    CkSsh ssh = new CkSsh();
    int sshPort = 22;
    success = ssh.Connect("ssh.example.com",sshPort);
    if (success == false) {
        Log.i(TAG, ssh.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 = ssh.AuthenticatePw("sshUser",sshPassword);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  Use the SSH connection as the transport for IMAP.  One SSH connection can be shared by
    //  several Chilkat objects because SSH supports multiple channels.
    success = imap.UseSsh(ssh);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    //  Now connect and log in to the IMAP server through the SSH connection.
    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."
  }
}