Tutorials Android How To Send Email In 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;
	}
}
Total
0
Shares
guest
9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
joy
joy
9 years ago

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

Jyotika
Jyotika
9 years ago

thanks for referring a nice tutorial site 🙂

Shamran
Shamran
8 years ago

Thanks a lot for your clear explanation.

Mayur Sojitra
Mayur Sojitra
7 years ago

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

JavaTechig
JavaTechig
7 years ago
Reply to  Mayur Sojitra

Not sure, if the device mail supports html mail.

Anita Shalu
Anita Shalu
7 years ago

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

Anita Shalu
Anita Shalu
7 years ago

thanks

Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago

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

Pradip Chindhe Patil
Pradip Chindhe Patil
4 years ago

How can i add my program to our webpage?

Previous Post
Android Tutorials

Sending SMS Message In Android

Next Post
Android Tutorials

Android Include Tag Layout Example

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
9
0
Would love your thoughts, please comment.x
()
x