Android Send Email With Examples
Android Send Email With Examples
In android, we can easily send an email from our android application using existing email clients
such as GMAIL, Outlook, etc. instead of building an email client from scratch.
Generally, the Intent object in android with proper action (ACTION_SEND) and data will help us
to launch the available email clients to send an email in our application.
In android, Intent is a messaging object which is used to request an action from another app
component such as activities, services, broadcast receivers, and content providers.
To send an email using the Intent object in android application, we need to write the code as
shown below.
If you observe above code we used multiple components to send email, those are
ACTION_SEND - It’s an activity action that specifies that we are sending some data.
putExtra - we use this putExtra() method to add extra information to our Intent. Here we can
add the following things.
The android Intent object is having different options such as EXTRA_CC, EXTRA_BCC,
EXTRA_HTML_TEXT, EXTRA_STREAM, etc. to add different options for an email client.
setType - We use this property to set the MIME type of data that we want to send. Here we used
“message/rfc822” and other MIME types are “text/plain” and “image/jpg”.
Android Send Email Example
Following is the example of sending an email with existing email clients using Intent in the
android application.
Create a new android application using android studio and give names as SendMailExample.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical" >
<EditText
android:id="@+id/txtTo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To"/>
<EditText
android:id="@+id/txtSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>
<EditText
android:id="@+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="Message"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Send"
android:id="@+id/btnSend"/>
</LinearLayout>
Now open our main activity file MainActivity.java
MainActivity.java
package com.sjkpgm.sendmailexample;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
ACTION_SEND - It’s an activity action that specifies that we are sending some data.
putExtra - we use this putExtra() method to add extra information to our Intent. Here we can
add the following things.
setType - We use this property to set the MIME type of data that we want to send. Here we used
“message/rfc822” and other MIME types are “text/plain” and “image/jpg”.
We need to add MIME type in our android manifest file for that open android manifest file
(AndroidManifest.xml) and write the code like as shown below
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" pa
ckage="com.tutlane.sendmailexample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="message/rfc822"/>
</intent-filter>
</activity>
</application>
</manifest>
If you observe above AndroidManifest.xml file we added following extra fields of Intent filters.
action - we use this property to define that the activity can perform SEND action.
category - we included the DEFAULT category for this activity to be able to receive implicit
intents.