Send Email Using Intent - Android Studio - Kotlin

In this example, we will send email using intent. See the following example:

activity_main.xml

Add the following code in the activity_main.xml file.

<?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:layout_margin="15dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/edt_email_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EFEFEF"
android:hint="Email To"
android:padding="10dp"
android:layout_margin="5dp"/>

<EditText
android:id="@+id/edt_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"
android:background="#EFEFEF"
android:padding="10dp"
android:layout_margin="5dp"/>

<EditText
android:id="@+id/edt_message"
android:layout_width="match_parent"
android:layout_height="200dp"
android:maxHeight="200dp"
android:gravity="top"
android:background="#EFEFEF"
android:hint="Message"
android:padding="10dp"
android:layout_margin="5dp"/>

<Button
android:id="@+id/btn_send_email"
android:layout_marginTop="10dp"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Send Mail"/>

</LinearLayout>

MainActivity.kt

Add the following code in the MainActivity.kt class.

package com.study.kotlinkatta

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

lateinit var btn_send_email: Button
lateinit var edt_email_to: EditText
lateinit var edt_subject: EditText
lateinit var edt_message: EditText

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

edt_email_to = findViewById(R.id.edt_email_to)
edt_subject = findViewById(R.id.edt_subject)
edt_message = findViewById(R.id.edt_message)

btn_send_email = findViewById(R.id.btn_send_email)
btn_send_email.setOnClickListener {

val str_email_to = edt_email_to.text.toString().trim()
val str_subject = edt_subject.text.toString().trim()
val str_message = edt_message.text.toString().trim()

if(str_email_to.equals("") || str_subject.equals("") || str_message.equals("")){
Toast.makeText(this, "Please enter all details", Toast.LENGTH_LONG).show()
}else {
func_sendEmail(str_email_to, str_subject, str_message)
}
}
}

private fun func_sendEmail(strEmailTo: String, strSubject: String, strMessage: String) {

val intent = Intent(Intent.ACTION_SEND)
intent.data = Uri.parse("mailto:")
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(strEmailTo))
intent.putExtra(Intent.EXTRA_SUBJECT, strSubject)
intent.putExtra(Intent.EXTRA_TEXT, strMessage)

try {
startActivity(Intent.createChooser(intent, "Choose Email Client..."))
}
catch (e: Exception){
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
}

}
}

Output:








Comments