Sample code for 30+ languages & platforms
Android™

Get a To Recipient's Address Only

See more Email Object Examples

Demonstrates the Chilkat Email.GetToAddr method, which returns only the address part (not the friendly-name part) of the Nth To recipient. The index is zero-based. This example adds two To recipients and prints each one's address.

Background: When you need the machine-usable part of a recipient — the actual user@domainGetToAddr returns it without the surrounding display name. This is what you want for validating addresses, removing duplicates, or checking against an allow/deny list, since the display name is free-form text and unreliable for identity.

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);

    //  Demonstrates the GetToAddr method, which returns only the address part (not the friendly
    //  name) of the Nth To recipient.  The index is zero-based.

    CkEmail email = new CkEmail();
    email.put_Subject("GetToAddr example");

    email.AddTo("Joe Smith","joe@example.com");
    email.AddTo("Jane Doe","jane@example.com");

    int n = email.get_NumTo();
    int i;
    for (i = 0; i <= n - 1; i++) {
        Log.i(TAG, "To " + String.valueOf(i) + " address: " + email.getToAddr(i));
        }


  }

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