Sample code for 30+ languages & platforms
Android™

Authenticate with the SMTP Server

See more SMTP Examples

Demonstrates the Chilkat MailMan.SmtpAuthenticate method, which authenticates with the SMTP server using the current SMTP property settings, such as SmtpUsername and SmtpPassword. It is typically called after SmtpConnect. This example connects and then authenticates.

Background: SMTP authentication is a separate step from connecting: after the EHLO handshake, the client proves its identity via the AUTH command using a mechanism the server offers (LOGIN, PLAIN, CRAM-MD5, XOAUTH2, etc.). Splitting connect and authenticate into explicit calls lets you pinpoint exactly which step fails — and hold an authenticated connection open to send many messages efficiently.

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.SmtpAuthenticate method, which authenticates with the SMTP server
    //  using the current SMTP property settings (such as SmtpUsername and SmtpPassword).  It is
    //  typically called after SmtpConnect.

    CkMailMan mailman = new CkMailMan();

    //  Configure the SMTP server connection.
    mailman.put_SmtpHost("smtp.example.com");
    mailman.put_SmtpPort(465);
    mailman.put_SmtpSsl(true);
    mailman.put_SmtpUsername("user@example.com");
    mailman.put_SmtpPassword("myPassword");

    //  Connect first, then authenticate.

    success = mailman.SmtpConnect();
    if (success == false) {
        Log.i(TAG, mailman.lastErrorText());
        return;
        }

    success = mailman.SmtpAuthenticate();
    if (success == false) {
        Log.i(TAG, mailman.lastErrorText());
        return;
        }

    Log.i(TAG, "Authenticated with the SMTP server.");

  }

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