Send Email using Intent | Android Studio | Java

How To Send Email Using Intent

In this article, we will learn how to send the Email using the intent. We will input the receipt email(s), subject, and message to send via email.

>>Watch For Kotlin

VIDEO


Step 1: Create a new project OR Open your project

Step 2: Code

AdroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <!--For Gmail Intent: Since Android 11 (API level 30), most user-installed apps are not visible by default.
    In your manifest, you must statically declare which apps you are going to get info about, as in the following:-->
    <queries>
        <package android:name="com.google.android.gm" />
    </queries>

....

</manifest>

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".MainActivity">

    <!--EditText: Input the recipient-->
    <EditText
        android:id="@+id/recipientEt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_edittext"
        android:hint="Recipient email(s)"
        android:inputType="textEmailAddress"
        android:padding="10dp"
        android:textColor="@color/colorBlack" />

    <!--EditText: Input the subject of email-->
    <EditText
        android:id="@+id/subjectEt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_edittext"
        android:hint="Subject"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:inputType="text|textCapSentences"
        android:padding="10dp"
        android:textColor="@color/colorBlack" />

    <!--EditText: Input the message-->
    <EditText
        android:id="@+id/messageEt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_edittext"
        android:gravity="start"
        android:hint="Enter message here..."
        android:inputType="text|textCapSentences"
        android:minHeight="150dp"
        android:padding="10dp"
        android:textColor="@color/colorBlack" />

    <!--Button:   Launch existing email clients to send email-->
    <Button
        android:id="@+id/sendEmailBtn"
        style="@style/Base.Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="send Email" />

</LinearLayout>

MainActivity.java
package com.technifysoft.emailintent;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    //declare views
    private EditText mRecipientEt, mSubjectEt, mMessageEt;
    private Button mSendEmailBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //initializing views with activity_main.xml
        mRecipientEt = findViewById(R.id.recipientEt);
        mSubjectEt = findViewById(R.id.subjectEt);
        mMessageEt = findViewById(R.id.messageEt);
        mSendEmailBtn = findViewById(R.id.sendEmailBtn);

        //button click to get input and call sendEmail method
        mSendEmailBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //get input from EditTexts and save in variables
                String recipient = mRecipientEt.getText().toString().trim(); //trim will remove space before and after the text
                String subject = mSubjectEt.getText().toString().trim();
                String message = mMessageEt.getText().toString().trim();

                //method call for email intent with these inputs as parameters
                sendEmail(recipient, subject, message);

            }
        });

    }

    private void sendEmail(String recipient, String subject, String message) {
        //call email share method
        String gmailPackage = "com.google.android.gm";
        // return true if gmail is installed
        boolean isGmailInstalled = isAppInstalled(gmailPackage);

        /*ACTION_SEND action to launch an email client installed on your Android device.*/
        Intent intent = new Intent(Intent.ACTION_SEND);
        // put recipient email in intent
        /* recipient is put as array because you may wanna send email to multiple emails
           so enter comma(,) separated emails, it will be stored in array*/
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient});
        //put subject of email
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        //put message of email in intent
        intent.putExtra(Intent.EXTRA_TEXT, message);
        if (isGmailInstalled) {
            intent.setType("text/html");
            intent.setPackage(gmailPackage);
            startActivity(intent);
        } else {
            // allow user to choose a different app to send email with
            intent.setType("message/rfc822");
            startActivity(Intent.createChooser(intent, "choose an email client"));
        }

    }

    private boolean isAppInstalled(String packageName) {
        try {
            getPackageManager().getApplicationInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

}

Step 3: Run Project

Output
Email Intent - Android Studio - Java

Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Kotlin

Add a Back Button to Action Bar Android Studio (Kotlin)