Android tutorial - How to Send Email in Android | Android Email - android app development - android studio - android development tutorial



 compose messages of gmail account app in android

What is Email?

  • Email is messages distributed by electronic means from one system user to one or more recipients via a network.
  • Previously starting Email Activity, you must know Email functionality with intent.
  • Intent is carrying data from one component to another component with-in the application or outside the application.
  • send an email from your application, you don’t have to implement an email client from the starting, but you can use an existing one like the default Email app providing from Android, Gmail, Outlook, K-9 Mail etc.
  • For this purpose, we need to write an Activity that launches an email client, using an implicit Intent with the right action and data.
  • In this eg, we are going to send an email from our app by using an Intent object that launches existing email clients.
  • In Android, you can use Intent.ACTION_SEND to call an existing email client to send an Email.
  • send email through android app
  • See following code snippets:
android codes - android tutorial - android sdk
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail@yahoo.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
click below button to copy the code from android tutorial team
  • This project is developed in Eclipse 3.7, and tested with Samsung Galaxy S2 (Android 2.3.3).

Run & test on real device only.

  • If you run this on emulator, you will hit error message: “No application can perform this action “. This code only works on real device.

Android Layout

  • File: res/layout/main.xml
android codes - android tutorial - android sdk
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewPhoneNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="To : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextTo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress" >

        <requestFocus />

    </EditText>

    <TextView
        android:id="@+id/textViewSubject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Subject : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextSubject"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         >
    </EditText>

    <TextView
        android:id="@+id/textViewMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Message : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="5" />

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send" />

</LinearLayout>
click below button to copy the code from android tutorial team

Activity

  • Full activity class to send an Email. Read the onClick() method, it should be self-explanatory.
android codes - android tutorial - android sdk
package com.wikitechy.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SendEmailActivity extends Activity {

	Button buttonSend;
	EditText textTo;
	EditText textSubject;
	EditText textMessage;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		buttonSend = (Button) findViewById(R.id.buttonSend);
		textTo = (EditText) findViewById(R.id.editTextTo);
		textSubject = (EditText) findViewById(R.id.editTextSubject);
		textMessage = (EditText) findViewById(R.id.editTextMessage);

		buttonSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			  String to = textTo.getText().toString();
			  String subject = textSubject.getText().toString();
			  String message = textMessage.getText().toString();

			  Intent email = new Intent(Intent.ACTION_SEND);
			  email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
			  //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
			  //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
			  email.putExtra(Intent.EXTRA_SUBJECT, subject);
			  email.putExtra(Intent.EXTRA_TEXT, message);

			  //need this to prompts email client only
			  email.setType("message/rfc822");

			  startActivity(Intent.createChooser(email, "Choose an Email client :"));

			}
		});
	}
}
click below button to copy the code from android tutorial team

Demo - android emulator - android tutorial

  • See default Mobile display, fill in the detail, and click on the “send” button.
 send messages from email in android
  • It will prompt your existing Email client to select.
 choose client account from gmail in android
  • In this case, we selected Gmail, and all previous filled in detail will be populated to Gmail client automatically.
 compose message to the account for email in android

Note

  • Android doesn’t provide API to send Email directly, you have to call the existing Email client to send Email.

Related Searches to How to Send Email in Android | Android Email