Home »
Android
Compose mail from your Android Device
In this article, we are going to learn about emails and how to compose a mail using Intent.ACTION_SEND through your android device?
Submitted by Manu Jemini, on February 17, 2018
In the example, below we are going to use the Intent and Bundle class to composing an email programmatically from your app with intent.
To implement this in your program you will need to import: android.content.Intent, android.net.Uri in your java file.
Then create two layout files first with a single TextView and Button. Button should be making like this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to call"
android:id="@+id/button"
android:onClick="email"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="161dp" />
In the Button above, we have put an onClick property and set it to email function which is in the java File. We assign on click function directly like this : android:onClick="email".
First we will make an Intent object of type intent.ACTION_SEND. Next, we set data to that object with the URI parse method.
After that we keep setting fields to the object such that EXTRS.SUBJECT etc.
eintent.putExtra(Intent.EXTRA_EMAIL, to);
In the end we log and send email successfully.
1) Java file:
package com.example.hp.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void email(View arg0){
Log.i("Send email", "");
String[] to ={""};
String[] cc ={""};
Intent eintent=new Intent(Intent.ACTION_SEND);
eintent.setData(Uri.parse("mailto:"));
eintent.setType("text/plain");
eintent.putExtra(Intent.EXTRA_EMAIL, to);
eintent.putExtra(Intent.EXTRA_CC, cc);
eintent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
eintent.putExtra(Intent.EXTRA_TEXT, "Compose mail");
startActivity(Intent.createChooser(eintent, "Send"));
finish();
Log.i("Email send sucessfully","");
}
}
2) XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hp.myapplication.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to Compose a mail"
android:id="@+id/button"
android:onClick="email"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="161dp" />
</RelativeLayout>
3) Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp.myapplication">
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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" />
</intent-filter>
</activity>
</application>
</manifest>
Output