How to send an Email from your Android App?

17 Jan, 2020
In this article, you will make a basic android application which can be used to send email through your android application.
You can do so with the help of Intent with action as ACTION_SEND with extra fields:
email id to which you want to send mail,
subject of email and
body of the email.
Basically Intent is a simple message object that is used to communicate between android components such as activities, content providers, broadcast receivers and services, here use to send the email.
This application basically contains one activity with EditText to take input of email address, subject and body of the email from user and button to send that email.
Step 1. activity_main.xmlactivity_main.xml contains a Relative Layout which contains three Edit texts for receiver mail id, other for the subject of the mail and last one for the body of the email and three TextViews for the label and a button for starting intent or sending mail:activity_main.xmlactivity_main.xml<?xml version="1.0" encoding="utf-8"?>  <!--Relative Layout--><RelativeLayout    xmlns:androclass="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context=".MainActivity">      <!--Edit text for email id-->    <EditText          android:id="@+id/editText1"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignParentRight="true"          android:layout_alignParentTop="true"          android:layout_marginRight="22dp"          android:layout_marginTop="18dp"        />        <!--Edit text for email subject-->    <EditText          android:id="@+id/editText2"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_below="@+id/editText1"          android:layout_alignLeft="@+id/editText1"          android:layout_marginTop="20dp"          />          <!--Edit text for email body-->    <EditText          android:id="@+id/editText3"          android:layout_width="wrap_content"          android:layout_height="wrap_content"         android:layout_marginTop="30dp"          android:layout_below="@+id/editText2"          android:layout_alignLeft="@+id/editText2"          />        <!--text Views for label-->    <TextView          android:id="@+id/textView1"         android:textColor="#0F9D58"        android:text="Send To:"         android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignBaseline="@+id/editText1"          android:layout_alignBottom="@+id/editText1"          android:layout_alignParentLeft="true"          />        <TextView          android:id="@+id/textView2"         android:textColor="#0F9D58"        android:text="Email Subject:"         android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignBaseline="@+id/editText2"          android:layout_alignBottom="@+id/editText2"          android:layout_alignParentLeft="true"          />        <TextView          android:id="@+id/textView3"          android:textColor="#0F9D58"        android:text="Email Body:"        android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignBaseline="@+id/editText3"          android:layout_alignBottom="@+id/editText3"           />        <!--Button to send email-->    <Button          android:id="@+id/button"          android:text="Send email!!"        android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignLeft="@+id/editText3"          android:layout_below="@+id/editText3"          android:layout_marginLeft="76dp"          android:layout_marginTop="20dp"           />    </RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>  <!--Relative Layout--><RelativeLayout    xmlns:androclass="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context=".MainActivity">      <!--Edit text for email id-->    <EditText          android:id="@+id/editText1"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignParentRight="true"          android:layout_alignParentTop="true"          android:layout_marginRight="22dp"          android:layout_marginTop="18dp"        />        <!--Edit text for email subject-->    <EditText          android:id="@+id/editText2"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_below="@+id/editText1"          android:layout_alignLeft="@+id/editText1"          android:layout_marginTop="20dp"          />          <!--Edit text for email body-->    <EditText          android:id="@+id/editText3"          android:layout_width="wrap_content"          android:layout_height="wrap_content"         android:layout_marginTop="30dp"          android:layout_below="@+id/editText2"          android:layout_alignLeft="@+id/editText2"          />        <!--text Views for label-->    <TextView          android:id="@+id/textView1"         android:textColor="#0F9D58"        android:text="Send To:"         android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignBaseline="@+id/editText1"          android:layout_alignBottom="@+id/editText1"          android:layout_alignParentLeft="true"          />        <TextView          android:id="@+id/textView2"         android:textColor="#0F9D58"        android:text="Email Subject:"         android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignBaseline="@+id/editText2"          android:layout_alignBottom="@+id/editText2"          android:layout_alignParentLeft="true"          />        <TextView          android:id="@+id/textView3"          android:textColor="#0F9D58"        android:text="Email Body:"        android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignBaseline="@+id/editText3"          android:layout_alignBottom="@+id/editText3"           />        <!--Button to send email-->    <Button          android:id="@+id/button"          android:text="Send email!!"        android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignLeft="@+id/editText3"          android:layout_below="@+id/editText3"          android:layout_marginLeft="76dp"          android:layout_marginTop="20dp"           />    </RelativeLayout>
Step 2. MainActivity.javaIn Main activity Intent object is created and its action is defined to ACTION_SEND to send email, with Intent three extra fields are also added using putExtra function.These fields are:Email of receiverSubject of emailBody of emailsetOnClickListener is attached to button with intent object in it to make intent with action as ACTION_SEND to send email and intent type as shown in code.Here is complete java code to send email through intent from android application:MainActivity.javaMainActivity.javapackage com.geeksforgeeks.phonecall;  import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.content.Intent;import android.widget.EditText;import android.view.View;import android.view.View.OnClickListener;import android.net.Uri;import android.widget.Button;  public class MainActivity extends AppCompatActivity {      // define objects for edit text and button    Button button;    EditText sendto, subject, body;      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // Getting instance of edittext and button        sendto = findViewById(R.id.editText1);        subject = findViewById(R.id.editText2);        body = findViewById(R.id.editText3);        button = findViewById(R.id.button);          // attach setOnClickListener to button        // with Intent object define in it        button.setOnClickListener(new OnClickListener() {              @Override            public void onClick(View view)            {                String emailsend = sendto.getText().toString();                String emailsubject = subject.getText().toString();                String emailbody = body.getText().toString();                  // define Intent object                // with action attribute as ACTION_SEND                Intent intent = new Intent(Intent.ACTION_SEND);                  // add three fiels to intent using putExtra function                intent.putExtra(Intent.EXTRA_EMAIL,                                new String[] { emailsend });                intent.putExtra(Intent.EXTRA_SUBJECT, emailsubject);                intent.putExtra(Intent.EXTRA_TEXT, emailbody);                  // set type of intent                intent.setType("message/rfc822");                  // startActivity with intent with chooser                // as Email client using createChooser function                startActivity(                    Intent                        .createChooser(intent,                                       "Choose an Email client :"));            }        });    }}
Email of receiver
Subject of email
Body of email
setOnClickListener is attached to button with intent object in it to make intent with action as ACTION_SEND to send email and intent type as shown in code.
Here is complete java code to send email through intent from android application:
MainActivity.java
package com.geeksforgeeks.phonecall;  import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.content.Intent;import android.widget.EditText;import android.view.View;import android.view.View.OnClickListener;import android.net.Uri;import android.widget.Button;  public class MainActivity extends AppCompatActivity {      // define objects for edit text and button    Button button;    EditText sendto, subject, body;      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // Getting instance of edittext and button        sendto = findViewById(R.id.editText1);        subject = findViewById(R.id.editText2);        body = findViewById(R.id.editText3);        button = findViewById(R.id.button);          // attach setOnClickListener to button        // with Intent object define in it        button.setOnClickListener(new OnClickListener() {              @Override            public void onClick(View view)            {                String emailsend = sendto.getText().toString();                String emailsubject = subject.getText().toString();                String emailbody = body.getText().toString();                  // define Intent object                // with action attribute as ACTION_SEND                Intent intent = new Intent(Intent.ACTION_SEND);                  // add three fiels to intent using putExtra function                intent.putExtra(Intent.EXTRA_EMAIL,                                new String[] { emailsend });                intent.putExtra(Intent.EXTRA_SUBJECT, emailsubject);                intent.putExtra(Intent.EXTRA_TEXT, emailbody);                  // set type of intent                intent.setType("message/rfc822");                  // startActivity with intent with chooser                // as Email client using createChooser function                startActivity(                    Intent                        .createChooser(intent,                                       "Choose an Email client :"));            }        });    }}
Output:
android
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java