How to send Email in Android

In Android, you can use Intent.ACTION_SEND to call an existing email client to send an Email.

See following code snippets :


	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 :"));

P.S 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 work on real device.

1. Android Layout

File : res/layout/main.xml


<?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>

2. Activity

Full activity class to send an Email. Read the onClick() method, it should be self-explanatory.


package com.mkyong.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 :"));
			  
			}
		});
	}
}

3. Demo

See default scree, fill in the detail, and click on the “send” button.

send email in android

It will prompts your existing Email client to select.

send email in android

In this case, i selected Gmail, and all previous filled in detail will be populated to Gmail client automatically.

send email in android
Note
Android does not provide API to send Email directly, you have to call the existing Email client to send Email.

Download Source Code

Download it – Android-Send-Email-Example.zip (16 KB)

References

  1. Android Intent ACTION_SEND Javadoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
W88
Guest
W88

thanks for share your information,This info is very useful for me

nonton movie
Guest
nonton movie

I prefer to use an exist sender account to send email

188BETLinks
Guest
188BETLinks

It’? awesome t? g? to see this web site and reading the views of all friends concerning thi? post, while I am al?o keen of ?etting experience

Fauzi21
Guest
Fauzi21

Thank you for sharing, but i have a question. How to make a confirmation before we send an e-mail? Like AlertDialog?

krishnanshu tyagi
Guest
krishnanshu tyagi

Can anyone tell me that how can I send the mail with out the use of Gmail or Email app.
Means other app like Gmail but do not use Gmail or Email???

timi
Guest
timi

thank you for your Examples is very useful to me.

August
Guest
August

I prefer to use an exist sender account to send email , I do not expect APP user know how to setup their sending account , please help me to build the source code that send mail directly , instead of Via mobile client mailbox.

GiapCN
Guest
GiapCN

Thanks for sharing.

Abdulla
Guest
Abdulla

Very Use Full

disocia
Guest
disocia

Thank you for this, so much. I am a beginner android developer. How would I add an option to send picture attatchments with the email?

Liza
Guest
Liza

Nice tutorial.Plz post Tutorial on how to send email with attachments.

Harpal Singh
Guest
Harpal Singh

Intent intent = new Intent(Intent.ACTION_SEND);

intent.setType(“text/plain”);

intent.putExtra(Intent.EXTRA_EMAIL,

“emailaddress@emailaddress.com”);

intent.putExtra(Intent.EXTRA_SUBJECT, “Subject”);

intent.putExtra(

Intent.EXTRA_TEXT,

“attachment”

startActivity(Intent.createChooser(intent, “Send Email”));

André Azevedo Costa
Guest
André Azevedo Costa

How to send an email with attached files?

Brandon
Guest
Brandon

Wow Incredible!

karishma
Guest
karishma

using real devices also it shows the same error,”No application can perform this action“;

KOAPPINFO KLC
Guest
KOAPPINFO KLC

great tutorial ! would like to add an attachement to the mail. How do i handle that?

Neha
Guest
Neha

I used startActivityForResult to send the mail but getting result code as 0 everytime, even when the mail is received by the recepient. How can i get the correct result code.

Aswanth
Guest
Aswanth

How to send an e mail from a application without using “create choose”r (G mail as default) by calling intent??

sina
Guest
sina

Hi, my message body is a TableLayout not a text view how can i do that ?

Aruna
Guest
Aruna

hi,
your examples very useful to me.

thank you for sharing your knowledge.

balaji
Guest
balaji

hey its work fine but its not getting the to,subject,message field to the gmail to,subject,message field can any one just help me to sort it out

surya
Guest
surya

hi i want a mail app with any mail client.i dont have any applications in device.can tell me how can i implement it.

Emre
Guest
Emre

How do it know your email address?I can see another email address but yours can’t.

ali
Guest
ali

hi how to create live wallpaper for android plz give example

Monica
Guest
Monica

Thanks!! it really helped!!

ayaz
Guest
ayaz

error after clicking send button chose an email client :
no application can perform this action plzz help thanks

Vicente
Guest
Vicente

You should try this on a real device because the emulator won’t be able to start the activity.

David McGuinness
Guest
David McGuinness

Hi

thanks very much, excellent tutorial. works really well. however is it possible to have numerous message fields in place of just the one message text area. I was hoping that the user would be able to populate numerous fields and when the email was read by the recipient the messages would be read as one full message.

thanks in advance

Dave

nero
Guest
nero

thank you very much :)

kuldeep
Guest
kuldeep

i waant to get senders email address from recieved mail.how can i get that ?

farhan shah
Guest
farhan shah

Nice and Awsome Tutorials..Thumbs up!

trackback
Sending Emails in android app : Android Community - For Application Development

[…] Source : tutorial. […]