How to Send an Email From an Android Application?
In this article, you will make a basic Android Application that can be used to send email through your android application. You can do so with the help of Intent with action as ACTION_SEND with extra fields:
- email id to which you want to send mail,
- the subject of the email and
- body of the email.
Basically Intent is a simple message object that is used to communicate between android components such as activities, content providers, broadcast receivers, and services, here use to send the email. This application basically contains one activity with EditText to take input of email address, subject, and body of the email from the user and button to send that email.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with the XML Files
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail. This file contains a Relative Layout which contains three Edit texts for receiver mail id, another for the subject of the mail, and last one for the body of the email, and three TextViews for the label and a button for starting intent or sending mail:
Step 3: Working with the MainActivity File
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.
In MainActivity Intent object is created and its action is defined to ACTION_SEND to send an email, with Intent three extra fields are also added using the putExtra function. These fields are:
- Email of receiver
- Subject of email
- Body of email
setOnClickListener is attached to a button with the intent object in it to make intent with action as ACTION_SEND to send email and intent type as shown in code.
Here is the complete code to send email through intent from the android application: