Android™
Android™
Use an Existing Socket SSH Tunnel
See more SMTP Examples
Demonstrates the Chilkat MailMan.UseSshTunnel method, which configures MailMan to use an existing SSH tunnel provided by a Socket object for its SMTP and POP3 connections. This example opens and authenticates a tunnel on a Socket, hands it to MailMan, and then verifies an SMTP login through it.
Background: Sharing one already-established SSH tunnel across objects avoids opening (and authenticating) a separate SSH connection for every component that needs to reach the remote network. Here the tunnel lives on a
Socket object, which MailMan then routes its SMTP/POP3 traffic through. It is the Socket-based sibling of UseSsh, which shares an Ssh object instead.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 MailMan.UseSshTunnel method, which configures MailMan to use an existing
// SSH tunnel provided by a Socket object for its SMTP and POP3 connections.
// Open and authenticate the SSH tunnel using a Socket object.
CkSocket sshTunnel = new CkSocket();
success = sshTunnel.SshOpenTunnel("ssh.example.com",22);
if (success == false) {
Log.i(TAG, sshTunnel.lastErrorText());
return;
}
success = sshTunnel.SshAuthenticatePw("sshUser","sshPassword");
if (success == false) {
Log.i(TAG, sshTunnel.lastErrorText());
return;
}
CkMailMan mailman = new CkMailMan();
// Configure the SMTP server connection. These connections will travel through the tunnel.
mailman.put_SmtpHost("smtp.example.com");
mailman.put_SmtpPort(465);
mailman.put_SmtpSsl(true);
mailman.put_SmtpUsername("user@example.com");
mailman.put_SmtpPassword("myPassword");
// Tell MailMan to route its connections through the existing tunnel.
success = mailman.UseSshTunnel(sshTunnel);
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
// SMTP operations now travel through the shared SSH tunnel.
success = mailman.VerifySmtpLogin();
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
Log.i(TAG, "Authenticated with the SMTP server through the SSH tunnel.");
}
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."
}
}