Android™
Android™
Close the SSH Tunnel
See more POP3 Examples
Demonstrates the Chilkat MailMan.SshCloseTunnel method, which closes the SSH tunnel used for SMTP or POP3 communication. This example opens and authenticates a tunnel, performs a mail operation through it, and then closes it.
Background: An SSH tunnel holds an open connection to the SSH server for as long as it is needed, so closing it releases both that connection and the resources on the SSH server. The natural pattern is open → authenticate → do mail work → close. It is the counterpart to
SshOpenTunnel, and is separate from closing the mail connections themselves (CloseSmtpConnection / Pop3EndSession), which ride inside the tunnel.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.SshCloseTunnel method, which closes the SSH tunnel used for SMTP
// or POP3 communication.
CkMailMan mailman = new CkMailMan();
// Configure the POP3 server connection.
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 and authenticate the SSH tunnel.
success = mailman.SshOpenTunnel("ssh.example.com",22);
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
success = mailman.SshAuthenticatePw("sshUser","sshPassword");
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
// ... perform mail operations through the 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 SSH tunnel when finished with it.
success = mailman.SshCloseTunnel();
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
Log.i(TAG, "SSH tunnel closed.");
}
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."
}
}