Android

How To Send Email In Android

The code sample below will help you understand “How To Send Email In Android” using EmailIntent in android. In android we can use Intent.ACTION_SEND to call an existing email client to send an Email. If no email clients are configured then,android system displays “No application can perform this action” error.

Sending Email In Android example

Here is my layout as shown in the screenshots below

 sending email in android

activity_main.xml

<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"
    android:padding="5dp" >

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

    <EditText
        android:id="@+id/toEmail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter email" >
    </EditText>

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

    <EditText
        android:id="@+id/subject"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Subject" >
    </EditText>

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

    <EditText
        android:id="@+id/emailBody"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:hint="Enter mail body"
        android:inputType="textMultiLine"
        android:lines="5" />

</LinearLayout>

In my code example, I have used ActionBar commands for send and clear option.

main_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

     <item
        android:id="@+id/menu_clear"
        android:icon="@android:drawable/ic_menu_close_clear_cancel"
        android:showAsAction="ifRoom|withText"
        android:title="Clear"/>

    <item
        android:id="@+id/menu_send"
        android:icon="@android:drawable/ic_menu_send"
        android:showAsAction="ifRoom|withText"
        android:title="Send"/>

</menu>

Now, check out the activity code

MainActivity.java

public class MainActivity extends Activity {

	private EditText toEmail = null;
	private EditText emailSubject = null;
	private EditText emailBody = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		toEmail = (EditText) findViewById(R.id.toEmail);
		emailSubject = (EditText) findViewById(R.id.subject);
		emailBody = (EditText) findViewById(R.id.emailBody);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_clear:
			toEmail.setText("");
			emailBody.setText("");
			emailSubject.setText("");
			break;
		case R.id.menu_send:
			String to = toEmail.getText().toString();
			String subject = emailSubject.getText().toString();
			String message = emailBody.getText().toString();

			Intent email = new Intent(Intent.ACTION_SEND);
			email.putExtra(Intent.EXTRA_EMAIL, 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"));

			break;
		}
		return true;
	}
}
Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our Comments Policy.
  • Nilanchala Panigrahy

    You can do that by providing the email app package name directly. However that is not recommended. Following code will help you.

    Intent emailIntent = getPackageManager().getLaunchIntentForPackage(“com.android.email”); // or check with com.google.android.gm
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, message);
    startActivity(emailIntent);

  • Anita Shalu

    thanks

  • Anita Shalu

    I really dont know how to thank you..this coding helped me in such a way…thanks a lot…

  • i want format mail
    i can’t set table in mail using html

    • Not sure, if the device mail supports html mail.

  • Shamran

    Thanks a lot for your clear explanation.

  • Jyotika

    thanks for referring a nice tutorial site :)

  • joy

    Can you provide any code to validate mail id? Ur code helps for sending mail but validation is missing.