Android Email Intent Tutorial — How to Send Email From Within Your App | kotlin

Devendra Chavan
3 min readAug 11, 2023

--

Hello, and Welcome to the #CodingWithDev​ channel! In this tutorial, you will learn how to Send an email from an Android app using intents in Android Studio using Kotlin.

The tutorial covers the following aspects:
• Create a new Android Studio project with an empty activity template.
• Design the UI using XML layout files with EditText fields for recipient’s email, subject, and message, along with a “Send Email” button.
• Accessing the UI components (EditText and Button) in the MainActivity.kt file using findViewById.
• Adding a click listener to the “Send Email” button to trigger the email sending process.
• Providing an alternative approach using ACTION_SEND to compose an email. • Adding recipient’s email, subject, and message as extra data to the intent using putExtra.
• Using Intent.createChooser to let the user choose an email client to use for sending the email.

Video

Step 1: Create a new Project or open new project

Step 2: activity_main.xml layout file code

<?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>

Step 3: MainActivity.kt Activity code

import android.content.Intent
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

//button click to get input and call sendEmail method
sendEmailBtn.setOnClickListener {
//get input from EditTexts and save in variables
val recipient = recipientEt.text.toString().trim()
val subject = subjectEt.text.toString().trim()
val message = messageEt.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) {
/*ACTION_SEND action to launch an email client installed on your Android device.*/
val mIntent = Intent(Intent.ACTION_SEND)
/*To send an email you need to specify mailto: as URI using setData() method
and data type will be to text/plain using setType() method*/
mIntent.data = Uri.parse("mailto:")
mIntent.type = "text/plain"
// 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*/
mIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
//put the Subject in the intent
mIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
//put the message in the intent
mIntent.putExtra(Intent.EXTRA_TEXT, message)


try {
//start email intent
startActivity(Intent.createChooser(mIntent, "Choose Email Client..."))
}
catch (e: Exception){
//if any thing goes wrong for example no email client application or any exception
//get and show exception message
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
}

}
}

Step 4: Run project and Output

--

--