Send email with intent

To send an email using Kotlin, you can utilize the Intent class from the Android framework. The Intent class allows you to perform various actions, including sending emails. Here are the steps to send an email using Kotlin:

Step 1: Define the recipient's email address To send an email, you need to specify the recipient's email address. You can use a variable to store the email address, for example:

val recipientEmail = "[email protected]"

Step 2: Create a new Intent with ACTION_SENDTO Next, create a new Intent object and specify the action as ACTION_SENDTO. This action indicates that you want to send data to a specific recipient. Set the data URI to include the recipient's email address, like this:

val emailIntent = Intent(Intent.ACTION_SENDTO).apply {
    data = Uri.parse("mailto:$recipientEmail")
}

Step 3: Set the email subject and body (optional) If you want to pre-fill the email subject and body, you can set them using the EXTRA_SUBJECT and EXTRA_TEXT extras of the Intent:

emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject of the email")
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body of the email")

Step 4: Start the email activity To start the email activity and allow the user to choose an email client to send the email, use the startActivity method with the emailIntent:

startActivity(emailIntent)

Step 5: Handling errors (optional) If you want to handle errors that may occur during the email sending process, such as if the user does not have an email client installed, you can wrap the startActivity call with a try-catch block and handle any exceptions that are thrown.

That's it! With these steps, you can send an email using Kotlin by utilizing the Intent class. Remember to handle any errors that may occur and ensure that the necessary permissions are granted in your AndroidManifest.xml file.