Sample code for 30+ languages & platforms
Android™

IMAP Login using SecureString Credentials

See more IMAP Examples

Demonstrates the Chilkat Imap.LoginSecure method, which authenticates a connected session using SecureString credentials. The first argument is the login name and the second is the password. This is the secure-string counterpart of Login; the mechanism is chosen by the AuthMethod property.

Background: A SecureString keeps the credential encrypted in memory and helps avoid leaving plaintext copies lying around in the process, which is why the password should come from an interactive prompt, an environment variable, or a secrets vault — never a hard-coded literal. For XOAUTH2, pass the login name in the first argument and the raw access token (without a Bearer prefix) in the second.

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.LoginSecure method, which authenticates the session using SecureString
    //  credentials.  The 1st argument is the login name and the 2nd is the password.

    CkImap imap = new CkImap();

    imap.put_Ssl(true);
    imap.put_Port(993);

    success = imap.Connect("imap.example.com");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    //  The password is not hard-coded in source.  Insert code here to obtain it from an
    //  interactive prompt, environment variable, or a secrets vault.
    String password;

    //  Place the login name and password into SecureString objects.
    CkSecureString secureLogin = new CkSecureString();
    secureLogin.Append("user@example.com");
    CkSecureString securePassword = new CkSecureString();
    securePassword.Append(password);

    success = imap.LoginSecure(secureLogin,securePassword);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    Log.i(TAG, "Logged in securely.");

    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."
  }
}