Sample code for 30+ languages & platforms
Android™

Open an SSH Tunnel for POP3

See more POP3 Examples

Demonstrates the Chilkat MailMan.SshOpenTunnel method, which connects to an SSH server and opens an SSH tunnel for MailMan's SMTP or POP3 connections. The first argument is the SSH server's hostname or IP address and the second is the SSH port. This example opens a tunnel, authenticates, performs a POP3 operation through it, and closes the tunnel.

Background: An SSH tunnel (port forwarding) wraps another protocol's traffic inside an encrypted SSH connection. Here, MailMan's POP3 or SMTP session is carried through the SSH server rather than connecting directly. This is useful when a mail server is only reachable from inside a network you can reach via SSH, or when policy requires all traffic to traverse a bastion host. Opening the tunnel is step one; you must then authenticate to the SSH server before the tunnel carries traffic.

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 MailMan.SshOpenTunnel method, which connects to an SSH server and opens
    //  an SSH tunnel for MailMan's SMTP or POP3 connections.  The 1st argument is the SSH server
    //  hostname or IP address, and the 2nd is the SSH port.

    CkMailMan mailman = new CkMailMan();

    //  Configure the POP3 server connection.  These connections will travel through the tunnel.
    mailman.put_MailHost("pop.example.com");
    mailman.put_MailPort(995);
    mailman.put_PopSsl(true);
    mailman.put_PopUsername("user@example.com");
    mailman.put_PopPassword("myPassword");

    //  Open the SSH tunnel (port 22 is the standard SSH port).
    success = mailman.SshOpenTunnel("ssh.example.com",22);
    if (success == false) {
        Log.i(TAG, mailman.lastErrorText());
        return;
        }

    //  Authenticate with the SSH server.
    success = mailman.SshAuthenticatePw("sshUser","sshPassword");
    if (success == false) {
        Log.i(TAG, mailman.lastErrorText());
        return;
        }

    //  POP3 operations now travel through the SSH tunnel.
    int count = mailman.GetMailboxCount();
    if (count < 0) {
        Log.i(TAG, "Failed to get the mailbox count.");
        }
    else {
        Log.i(TAG, "Mailbox count: " + String.valueOf(count));
        }

    //  Close the tunnel when finished.
    success = mailman.SshCloseTunnel();
    if (success == false) {
        Log.i(TAG, mailman.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."
  }
}