Introduction
Email is the electronic transmission of messages from one system user to one or more recipients through a network.
In this blog, we will be learning about how to send an email on Android, Action intent type, MIME meaning, and much more.
Before jumping directly on how Android Studio does this, first knowing the basic knowledge of Kotlin and how to use it is strongly recommended.
So, let us start reading about how sending of emails works on Android.
Emails in Android
Before you begin your email activity, you must first understand the operation of Email with Intent. Within the application or outside the application, Intent transports data from one component to another.
You don't have to build an email client from the ground up to send an email from your app; instead, you may utilize one that already exists, such as Android's default Email app, Gmail, Outlook, K-9 Mail, and so on. To accomplish this, we'll need to create an Activity that launches an email client with the appropriate action and data based on an implicit Intent. In this example(later in this blog), we'll use an Intent object to send an email from our app, which will start with existing email clients.
You can also read about, android operating system
Intent Object for Sending Email
The sections below explain the various aspects of our Intent object that are required to send an email.
For Action In Intent Object
The ACTION_SEND action will be used to launch an email client installed on our Android device. The syntax for creating an intent using the ACTION_SEND action is as follows.
Syntax:
Intent emailIntent = new Intent(Intent.ACTION_SEND);
So, we can use the following as per the required action type.
- ACTION_SENDTO (use it for no attachment)
- ACTION_SEND (use it for one attachment)
- ACTION_SEND_MULTIPLE (use it for multiple attachments)
For MIME Type In Intent Object
To send an email we need to specify mailto: as URI using the setData() method and MIME type/data type will be set to text/plain using setType() method as follows:
Syntax:
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
For Extras In Intent Object
Android includes a built-in capability for adding TO, SUBJECT, CC, and TEXT fields which can be attached to the Intent before sending the Intent to the targeted email client. We can include the following extra fields in our Email as shown below.
- Intent.EXTRA_EMAIL: All "To" recipient email addresses are stored in this string array.
- Intent.EXTRA_CC: All "CC" recipient email addresses are stored in this string array.
- Intent.EXTRA_BCC: All "BCC" recipient email addresses are stored in this string array.
- Intent.EXTRA_SUBJECT: The email subject as a string.
- Intent.EXTRA_TEXT: A string containing the Email's body.
- Intent.EXTRA_STREAM: Uri directing attention to the attachment. Instead, this should be an ArrayList containing multiple Uri objects If we are using the ACTION_SEND_MULTIPLE action.
- EXTRA_TITLE: When used with an ACTION_CHOOSER, a CharSequence dialog title is sent to the user.
Here's the syntax of how to add extra data to your Intent.
Syntax:
myEmailIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{"enter Recipient here"});
myEmailIntent.putExtra(Intent.EXTRA_SUBJECT, "type subject here");
myEmailIntent.putExtra(Intent.EXTRA_TEXT , "content of Message Body");
Now, let us see the implementation part of Sending Emails with a working example given below.