Beginner 10 min Java Full project

Send an Email with an Intent in Android Studio

Send what the user typed as an email, using the phone's own email app. A short lesson on implicit Intents.

What you'll learn

  • Build an email with ACTION_SENDTO
  • Add recipient, subject and message
  • Let the user choose their email app

Before you start

  • Android Studio (any recent version)
  • An emulator or an Android phone

How it fits together

  1. 1Fill in the form
  2. 2ACTION_SENDTO mailto:
  3. 3Email app opens
  4. 4User taps Send

Build it step by step

1. Create the project

In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24.

2. Build the form

Three Material text fields: To, Subject and a multi-line Message, plus a Send button.

3. Check the address

Patterns.EMAIL_ADDRESS checks the address looks right before opening anything.

4. Hand it to an email app

Create an Intent with ACTION_SENDTO and Uri.parse("mailto:"). Only email apps can handle mailto:, so chat apps don't appear. Add the address, subject and message as extras.

5. Let the user choose

Intent.createChooser lists every email app installed. If there are none, catch ActivityNotFoundException and show a message.

6. Run it

Fill in the form and tap Send: your email app opens with everything filled in.

Why use an Intent?

Your app never needs the user's email password or a mail server. The email app they already use does the sending.

What's improved over the video

Only email apps appear in the list, the address is checked, and phones with no email app get a clear message.

Application code

Select a file to see its code and where it belongs.

This code uses package com.example.emailintent. Change that first line to your own package, the one at the top of your MainActivity.

Replace fileapp/src/main/java/com/example/emailintent/MainActivity.java

Checks the address and hands the email to the phone's email app.

MainActivity.java
package com.example.emailintent;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Patterns;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

/**
 * Writes an email and hands it to the phone's email app with an implicit Intent.
 * The app never needs your email password: the email app does the sending.
 */
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Screens.setUp(this, R.layout.activity_main);

        TextInputLayout toLayout = findViewById(R.id.toLayout);
        TextInputEditText to = findViewById(R.id.to);
        TextInputEditText subject = findViewById(R.id.subject);
        TextInputEditText message = findViewById(R.id.message);
        Button send = findViewById(R.id.btnSend);

        send.setOnClickListener(v -> {
            String address = String.valueOf(to.getText()).trim();
            if (!Patterns.EMAIL_ADDRESS.matcher(address).matches()) {
                toLayout.setError(getString(R.string.error_email));
                return;
            }
            toLayout.setError(null);
            // ACTION_SENDTO with a mailto: address opens only email apps, not every sharing app.
            Intent email = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
                    .putExtra(Intent.EXTRA_EMAIL, new String[]{address})
                    .putExtra(Intent.EXTRA_SUBJECT, String.valueOf(subject.getText()))
                    .putExtra(Intent.EXTRA_TEXT, String.valueOf(message.getText()));
            try {
                startActivity(Intent.createChooser(email, getString(R.string.choose_app)));
            } catch (ActivityNotFoundException e) {
                Toast.makeText(this, R.string.no_email_app, Toast.LENGTH_LONG).show();
            }
        });
    }
}

Full source code