Android, Xml

Send email using intent | Email intent android

In the last article I have discussed about Make a phone call using intent in the Android Development Tutorial Series. Today we will understand the use of intent to send an email in android that will help you in android application development .

To send an email from your application, you don’t have to implement an email client instead of it you can use an existing one like Gmail, Outlook, etc. It is a type of implicit Intent. Here, it will send an email on defined email address from your app by using an Intent object that launches existing email clients.

In case if you are not aware of intent and their types in android check this article : Android Intent | Types of Intent

Here few lines of code is enough to send an email using Intent :

Intent sendEmail = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"+"email_id")); // mention an email id here
sendEmail.putExtra(Intent.EXTRA_SUBJECT, "subject"); //subject of the email
sendEmail.putExtra(Intent.EXTRA_TEXT, "body"); //body of the email
startActivity(Intent.createChooser(sendEmail, "Choose an email client from..."));
1. Create a New Project :

Create a new project in Android Studio from File > New Project and select Empty Activity from templates to send email in android .

2. Add two EditText and a Button to your layout :

Add two EditText and a button to your activity_main.xml or xml layout file .

<EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp"
        android:hint="Enter an email address"
        android:background="@android:drawable/editbox_background"
        android:layout_above="@+id/body"/>
    <EditText
        android:id="@+id/body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp"
        android:hint="Enter your message"
        android:background="@android:drawable/editbox_background"
        android:layout_above="@+id/send"/>
    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Email"
        android:layout_centerInParent="true"/>

Now open res -> layout -> activity_main.xml or xml layout file and just implement the following xml code .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity">

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp"
        android:hint="Enter an email address"
        android:background="@android:drawable/editbox_background"
        android:layout_above="@+id/body"/>
    <EditText
        android:id="@+id/body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp"
        android:hint="Enter your message"
        android:background="@android:drawable/editbox_background"
        android:layout_above="@+id/send"/>
    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Email"
        android:layout_centerInParent="true"/>
</RelativeLayout>
3. Implement setOnClickListener on button to make listener on it :
send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email_id = email.getText().toString();
                String msg = body.getText().toString();
                Intent sendEmail = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"+email_id)); // enter an email id here
                sendEmail.putExtra(Intent.EXTRA_SUBJECT, "subject"); //subject of the email
                sendEmail.putExtra(Intent.EXTRA_TEXT, msg); //body of the email
                startActivity(Intent.createChooser(sendEmail, "Choose an email client from..."));
            }
        });
4. Open your MainActivity.java and add the following code :
package net.technxt.testing;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class TestActivity extends AppCompatActivity {
    Button send;
    EditText email,body;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        send = (Button)findViewById(R.id.send);
        email = (EditText)findViewById(R.id.email);
        body = (EditText)findViewById(R.id.body);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email_id = email.getText().toString();
                String msg = body.getText().toString();
                Intent sendEmail = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"+email_id)); // enter an email id here
                sendEmail.putExtra(Intent.EXTRA_SUBJECT, "subject"); //subject of the email
                sendEmail.putExtra(Intent.EXTRA_TEXT, msg); //body of the email
                startActivity(Intent.createChooser(sendEmail, "Choose an email client from..."));
            }
        });
    }
}

Now run the project/app to test email intent in android app through android device or any Emulator .

Leave a Reply

Created by Bhashkar Poddar

%d bloggers like this: