Android: How to Open a Mail Composer View

Opening a Mail Composer View in Android

This article explains how to open the native email composer view on an Android device using your application.

Using the Intent System

Android provides a standard mechanism for launching activities, including the email composer, using the Intent system.

Steps:

  1. Create an Intent with the action ACTION_SEND.
  2. Set the TYPE of the intent to "text/plain" for plain text emails.
  3. Add extra data to the intent using putExtra():
    • EXTRA_EMAIL: Array of recipient email addresses.
    • EXTRA_SUBJECT: Subject of the email.
    • EXTRA_TEXT: Body of the email.
  4. Start the activity with startActivity().

Code Example:


Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"recipient@example.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email Body");

startActivity(Intent.createChooser(emailIntent, "Send email using..."));

Output:

This code will open the native email composer with the specified recipient, subject, and body, allowing the user to send the email.

Choosing an Email Client

The createChooser() method allows the user to select their preferred email client if multiple clients are installed on the device.

Code Example:


Intent emailIntent = new Intent(Intent.ACTION_SEND);
// ... (Set email intent extras as above)

startActivity(Intent.createChooser(emailIntent, "Send email using..."));

Output:

The device will display a dialog allowing the user to choose from available email clients to compose and send the email.

Handling Email Composer Result

You can use the startActivityForResult() method to receive the result from the email composer, indicating whether the email was sent successfully or not.

Steps:

  1. Use startActivityForResult() instead of startActivity().
  2. Override the onActivityResult() method in your Activity.
  3. Check the resultCode to determine the result.

Code Example:


private static final int EMAIL_REQUEST_CODE = 1;

// ...

Intent emailIntent = new Intent(Intent.ACTION_SEND);
// ... (Set email intent extras as above)

startActivityForResult(emailIntent, EMAIL_REQUEST_CODE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == EMAIL_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Email sent successfully
            Toast.makeText(this, "Email sent successfully", Toast.LENGTH_SHORT).show();
        } else if (resultCode == RESULT_CANCELED) {
            // Email sending cancelled
            Toast.makeText(this, "Email sending cancelled", Toast.LENGTH_SHORT).show();
        }
    }
}

Handling Attachments

To attach files to an email, use the EXTRA_STREAM extra with the Uri of the file.

Code Example:


Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822"); // for attaching a file
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///path/to/file.pdf"));
// ... (Set other extras as above)

startActivity(Intent.createChooser(emailIntent, "Send email using..."));

Table for Comparison

Feature Description
ACTION_SEND Starts the email composer with plain text content.
ACTION_SENDTO Starts the email composer with a pre-populated recipient email address.
EXTRA_EMAIL Sets the email recipient address(es).
EXTRA_SUBJECT Sets the email subject.
EXTRA_TEXT Sets the email body.
EXTRA_STREAM Attaches a file to the email.

By using the above methods, you can effectively open the mail composer from your Android application and allow users to easily send emails.


Leave a Reply

Your email address will not be published. Required fields are marked *