Android™
Android™
Authenticate an SSH Tunnel with a Password
See more SMTP Examples
Demonstrates the Chilkat MailMan.SshAuthenticatePw method, which authenticates with the SSH server using an SSH username and password after an SSH tunnel has been opened. The first argument is the SSH account name and the second is the corresponding password. This example opens a tunnel, authenticates, and sends SMTP traffic through it.
Background: Opening an SSH tunnel and authenticating to the SSH server are two separate steps, and note the SSH credentials are distinct from the mail account credentials — the SSH login gets you through the tunnel, while
SmtpUsername/SmtpPassword authenticate to the mail server at the far end. Password authentication is the simplest option; public-key authentication (SshAuthenticatePk) is generally preferred for automated systems.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.SshAuthenticatePw method, which authenticates with the SSH server
// using an SSH username and password after an SSH tunnel has been opened. The 1st argument
// is the SSH account name and the 2nd is the corresponding password.
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");
// Open the SSH tunnel first.
success = mailman.SshOpenTunnel("ssh.example.com",22);
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
// Authenticate with the SSH server using a username and password.
success = mailman.SshAuthenticatePw("sshUser","sshPassword");
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
Log.i(TAG, "SSH tunnel authenticated.");
// SMTP operations now travel through the SSH tunnel.
success = mailman.VerifySmtpLogin();
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
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."
}
}