Send Email using Intent | Android Studio | Kotlin

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 Java

VIDEO

Step 1: Create a new Project or open your project

Step 2: Code

AndroidManifest.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.kt
package com.technifysoft.emailintent

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class MainActivity1 : AppCompatActivity() {

    //declare views
    private lateinit var mRecipientEt: EditText
    private lateinit var mSubjectEt: EditText
    private lateinit var mMessageEt: EditText
    private lateinit var mSendEmailBtn: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        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 {
            //get input from EditTexts and save in variables
            val recipient = mRecipientEt.text.toString().trim()
            val subject = mSubjectEt.text.toString().trim()
            val message = mMessageEt.text.toString().trim()

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

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

        /*ACTION_SEND action to launch an email client installed on your Android device.*/
        val intent = 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, arrayOf(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.type = "text/html"
            intent.setPackage(gmailPackage)
            startActivity(intent)
        } else {
            // allow user to choose a different app to send email with
            intent.type = "message/rfc822"
            startActivity(Intent.createChooser(intent, "choose an email client"))
        }
    }

    private fun isAppInstalled(packageName: String): Boolean {
        return try {
            packageManager.getApplicationInfo(packageName, 0)
            true
        } catch (e: PackageManager.NameNotFoundException) {
            false
        }
    }
}

Step 3: Run Project

Output
Send Email using Intent - Android Studio - Kotlin

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)