Skip to main content

android - Intent to send an email


<?xml version="1.0" encoding="utf-8"?>
        android:text="Send An Email"


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private Button mButton;

    protected void onCreate(Bundle savedInstanceState) {

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(;
        mButton = (Button) findViewById(;

        // Set a click listener for the button
        mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // Specify the email related variables values
                String[] emails = new String[]{

                // emails for cc
                String[] emailsCC = new String[]{

                // emails for bcc
                String[] emailsBCC = new String[]{

                String mailSubject = "This is email subject";
                String mailBody = "This is the body of the email";

                        An intent is an abstract description of an operation to be performed. It can
                        be used with startActivity to launch an Activity, broadcastIntent to send it
                        to any interested BroadcastReceiver components, and startService(Intent)
                        or bindService(Intent, ServiceConnection, int) to communicate with
                        a background Service.

                        An Intent provides a facility for performing late runtime binding between
                        the code in different applications. Its most significant use is in the
                        launching of activities, where it can be thought of as the glue between
                        activities. It is basically a passive data structure holding an abstract
                        description of an action to be performed.
                    public static final String ACTION_SENDTO
                        Activity Action: Send a message to someone specified by the data.

                        Input : getData() is URI describing the target.
                        Output : nothing.
                        Constant Value : "android.intent.action.SENDTO"
                // Initialize a new Intent
                Intent intent = new Intent(Intent.ACTION_SENDTO);

                    public static final String ACTION_SEND
                        Activity Action: Deliver some data to someone else. Who the data is being
                        delivered to is not specified; it is up to the receiver of this action to
                        ask the user where the data should be sent.

                        When launching a SEND intent, you should usually wrap it in a chooser
                        (through createChooser(Intent, CharSequence)), which will give the proper
                        interface for the user to pick how to send your data and allow you to
                        specify a prompt indicating what they are doing.

                        Input: getType() is the MIME type of the data being sent. get*Extra can have
                        either a EXTRA_TEXT or EXTRA_STREAM field, containing the data to be sent.
                        If using EXTRA_TEXT, the MIME type should be "text/plain".

                        ACTION_SENDTO (for no attachment) or
                        ACTION_SEND (for one attachment) or
                        ACTION_SEND_MULTIPLE (for multiple attachments)
                // Alternate way to send email
                //Intent intent = new Intent(Intent.ACTION_SEND);

                    public Intent setType (String type)

                        Set an explicit MIME data type.
                        This is used to create intents that only specify a type and not data,
                        for example to indicate the type of data to return.

                        This method automatically clears any data that was previously set
                        (for example by setData(Uri)).

                        Note: MIME type matching in the Android framework is case-sensitive, unlike
                        formal RFC MIME types. As a result, you should always write your MIME types
                        with lower case letters, or use normalizeMimeType(String) or
                        setTypeAndNormalize(String) to ensure that it is converted to lower case.                */
                // Set the intent type

                // Alternate Intent type

                // Add extended data to the Intent
                    public static final String EXTRA_EMAIL
                        A String[] holding e-mail addresses that should be delivered to.

                        Constant Value: "android.intent.extra.EMAIL"

                    public static final String EXTRA_CC
                        A String[] holding e-mail addresses that should be carbon copied.

                        Constant Value: "android.intent.extra.CC"
                    public static final String EXTRA_BCC
                        A String[] holding e-mail addresses that should be blind carbon copied.

                        Constant Value: "android.intent.extra.BCC"
                // Define the email cc and bcc

                    public static final String EXTRA_SUBJECT
                        A constant string holding the desired subject line of a message.

                        Constant Value: "android.intent.extra.SUBJECT"

                    public static final String EXTRA_TEXT
                        A constant CharSequence that is associated with the Intent, used with
                        ACTION_SEND to supply the literal data to be sent. Note that this may be a
                        styled CharSequence, so you must use Bundle.getCharSequence() to retrieve it.

                        Constant Value: "android.intent.extra.TEXT"

                    public Intent setData (Uri data)
                        Set the data this intent is operating on. This method automatically clears
                        any type that was previously set by setType(String) or setTypeAndNormalize(String).

                        Note: scheme matching in the Android framework is case-sensitive, unlike the
                        formal RFC. As a result, you should always write your Uri with a lower case
                        scheme, or use normalizeScheme() or setDataAndNormalize(Uri) to ensure that
                        the scheme is converted to lower case.

                        data : Uri: The Uri of the data this intent is now targeting.
                        Intent : Returns the same Intent object, for chaining multiple calls into
                                 a single statement.
                // For only email app should handle this intent

                // Try to start the activity
                if (intent.resolveActivity(getPackageManager()) != null) {
                    // If there are no email client installed in this device
                    Toast.makeText(mContext,"No email client installed in this device.",Toast.LENGTH_SHORT).show();

Popular posts from this blog

How to use NumberPicker in Android

Table of contentsNumberPicker ExamplesetOnValueChangedListeneronValueChange()setWrapSelectorWheel()setMinValue()setMinValue()setMaxValue()NumberPicker with String ArraysetDisplayedValues()String Array1. NumberPicker Example activity_main.xml <RelativeLayout xmlns:android="" xmlns:tools="" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="#ffffff" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text="Select a number..." /> <NumberPicker android:id="@+id/np" …

How to change TextView font size in android

TextView font size TextView widget display text on android application. we can set or change TextView font size statically by declarative syntax in xml layout file or programmatically at run time in java file. even we can use an xml file source to define font size.

the following example code demonstrate us how can we define TextView font size in xml layout file and how can we uses dimens.xml to reference font size. in this example we did not changes any coding in java file, so here we only include the layout xml file and dimens.xml file. activity_main.xml <LinearLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="25dp" tools:context=".MainActivity" > <TextView android:id=&…

TextView new line (multiline) in android

TextView new line (multiline) TextView display text on android app. by default, TextView show text on a single line, and if it is long then TextView take more lines to display its text. android developers can generate a new line on TextView both programmatically and syntactically. android developers can make a multiline TextView without splitting text to multiline by android:minLines attribute.

the following android example code demonstrate us how can we syntactically create a new line on TextView widget by xml layout file and string resource file.

the simplest way to create a new line on TextView is android:text attribute. android:text attribute allow us to display text on android app. we can add a simple '\n' to TextView text where we want to start a new line. in this way we can create a multiline TextView widget in android app. we can assign android:text attribute value by this way android:text="Line1 \n Line2 \n Line3" for a TextView widget. this value will…