android-studio
  1. android-studio-send-email

Android Studio Send Email

Syntax

The syntax to send an email in Android Studio is as follows:

val recipientList = arrayOf("recipient1@example.com", "recipient2@example.com")
val ccList = arrayOf("cc1@example.com", "cc2@example.com")
val bccList = arrayOf("bcc1@example.com", "bcc2@example.com")
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_EMAIL, recipientList)
intent.putExtra(Intent.EXTRA_CC, ccList)
intent.putExtra(Intent.EXTRA_BCC, bccList)
intent.putExtra(Intent.EXTRA_SUBJECT, "Email subject")
intent.putExtra(Intent.EXTRA_TEXT, "Email body")
startActivity(Intent.createChooser(intent, "Choose email client"))

Example

Here's an example of sending an email in Android Studio:

val recipientList = arrayOf("john@example.com")
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_EMAIL, recipientList)
intent.putExtra(Intent.EXTRA_SUBJECT, "Hello!")
intent.putExtra(Intent.EXTRA_TEXT, "How are you doing?")
startActivity(Intent.createChooser(intent, "Choose email client"))

Output

The output of the above example will be an email being opened in the email client of the user's choice with the recipient pre-populated with "john@example.com", the subject pre-populated with "Hello!", and the body pre-populated with "How are you doing?".

Explanation

To send an email in Android Studio, we first create an Intent object with the action set to "ACTION_SEND". We then set the MIME type to "text/plain" using the "type" property of the Intent object. We can then add various extras to the Intent object such as "EXTRA_EMAIL", "EXTRA_CC", "EXTRA_BCC", "EXTRA_SUBJECT", and "EXTRA_TEXT".

The "EXTRA_EMAIL" extra takes an array of Strings representing email addresses of the recipients. The "EXTRA_CC" extra takes an array of Strings representing email addresses of the recipients to be carbon copied. The "EXTRA_BCC" extra takes an array of Strings representing email addresses of the recipients to be blind carbon copied. The "EXTRA_SUBJECT" extra takes a String representing the subject of the email. The "EXTRA_TEXT" extra takes a String representing the body of the email.

Finally, we start the activity with the Intent object by passing it to the "createChooser" method of the Intent class and starting the activity using the "startActivity" method.

Use

The Android Studio Send Email functionality can be used to send emails containing various types of content such as plain text, HTML, attachments, etc.

Some use cases for Android Studio Send Email include sending feedback or bug reports from within an app, sending promotional emails or newsletters, or sending emails with important information such as order confirmations, receipts, or invoices.

Important Points

  • In order to send an email, an email client must be installed on the device.
  • The user can choose to send the email using any email client installed on their device.
  • Sending an email can fail if there is no internet connection or if the email address or domain is invalid.

Summary

The Android Studio Send Email functionality allows developers to send emails from within apps using Intent objects. To send an email, an Intent object with the action set to "ACTION_SEND" is created, and various extras such as "EXTRA_EMAIL", "EXTRA_CC", "EXTRA_BCC", "EXTRA_SUBJECT", and "EXTRA_TEXT" are added. The activity is then started with the Intent object and the user is prompted to choose an email client to send the email with. It's important to note that an internet connection is required and the email address or domain must be valid for the email to be sent successfully.

Published on: