How to send an Email

7

This is a sample activity which shows How to compose a mail and send it with the default email configured on device. Last topic published on this forum was App Widget.

Underlying Algorithm:

Basic description of algorithm in step by step form:
1.) Create a Project MyEmailDemo.
2.) In Layout we will create two buttons :

(i) To send mail with plain text.
(ii) To send mail with Html Form.

3.) Replace the following code with res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
   <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
	<Button android:text="Send E-Mail with plain text" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content">
        </Button>
	<Button android:text="Send E-Mail with html form" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content">
        </Button>
</LinearLayout>

4.) We will use the following code in our activity to compose a email with plain text :

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String[] recipients = new String[]{"mymail@email.com", "",};
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample mail");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is a sample mail..");
emailIntent.setType("text/plain");
startActivity(Intent.createChooser(emailIntent, "Send mail client :"));
finish();

5.) We will use the following code in our activity to compose a email with html body :

Intent emailIntent2 = new Intent(android.content.Intent.ACTION_SEND);
String[] recipients2 = new String[]{"mymail@email.com", "",};
emailIntent2.putExtra(android.content.Intent.EXTRA_EMAIL, recipients2);
emailIntent2.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample mail");
emailIntent2.putExtra(android.content.Intent.EXTRA_TEXT,Html.fromHtml("<b><i>"+"new"+"</i></b><br><br><b><i>"+"html data"+"</i></b><br>"));
emailIntent2.setType("text/html");
startActivity(Intent.createChooser(emailIntent2, "Send mail client :"));
finish();

6.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. MyEmailDemo. Enter following information:
Project name: MyEmailDemo
Build Target: Android 2.1
Application name: MyEmailDemo
Package name: com.app. MyEmailDemo
Create Activity: MyEmailDemo

On Clicking Finish MyEmailDemo code structure is generated with the necessary Android Packages being imported along with MyEmailDemo.java. MyEmailDemo class will look like following:

package com.app.MyEmailDemo;

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

public class MyEmailDemo extends Activity implements OnClickListener {
/** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	Button mail = (Button)findViewById(R.id.button1);
	mail.setOnClickListener(this);
	Button mail2 = (Button)findViewById(R.id.button2);
	mail2.setOnClickListener(this);
   }

   @Override
   public void onClick(View v) {
	switch(v.getId())
	{
           case R.id.button1:
              Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
              String[] recipients = new String[]{"mymail@email.com", "",};
              emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
              emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample mail");
              emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is a sample mail..");
              emailIntent.setType("text/plain");
              startActivity(Intent.createChooser(emailIntent, "Send mail client :"));
              finish();
	      break;

           case R.id.button2:
              Intent emailIntent2 = new Intent(android.content.Intent.ACTION_SEND);
              String[] recipients2 = new String[]{"mymail@email.com", "",};
              emailIntent2.putExtra(android.content.Intent.EXTRA_EMAIL, recipients2);
              emailIntent2.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample mail");
              emailIntent2.putExtra(android.content.Intent.EXTRA_TEXT,Html.fromHtml("<b><i>"+"new"+"</i></b><br><br><b><i>"+"html data"+"</i></b><br>"));
              emailIntent2.setType("text/html");
              startActivity(Intent.createChooser(emailIntent2, "Send mail client :"));
              finish();
	      break;
	}
    }
}

You can download the source code of MyEmailDemo by clicking the link.

Output – The final output:

Did you enjoy this article?
Share
the
Love
Get Free Updates

7 Responses

  1. nikhil khatri says:

    Thanks it is really very help full

  2. Christopher says:

    Another thing

    I have download the project but when I am trying to load it at eclipse I have this error “Invalid project Description”

    Thanks

  3. Kalaiselvi G says:

    Hai ,

    Nice …thanks for this post.Is it possible to send both html and plain text through Intent?

  4. Donna says:

    Can you show a screen-shot of the email intent?
    Showing the html in email, right before the user hits “send”?

  5. reena says:

    Hi,
    Thanks a lot!
    It worked like a charm, only i changed setContentView(R.layout.main); to
    setContentView(R.layout.activity_main);
    Thanks again

  6. Meghna says:

    Thanks for the clean example… Even this http://www.compiletimeerror.com/2013/07/send-email-in-android-example.html might help, have a look..

  7. Hi there!

    Looks like a neat little project. I’ve never done any Android dev before, but I’m currently looking for a solution to an Android email problem. I recently switched to a Galaxy S2 as my phone/mobile office for running my business. But doing so created a problem. I have new order alert emails I receive when customers place orders. The email body includes order detail information (units ordered, color, size, etc) which is sent as one long string. In Outlook on my desktop, I have a sub that runs and parses the string each time Outlook receives a new order email. After the parse, the email body is re-written with the order details on separate lines. It makes the order email muuuuch easier to read. I’d like to do the same thing for my default Android email software. However, I don’t know where to begin. I know that Android is highly customizable. My question is: Is this possible? Judging from your project, it looks do-able. So question is, how do I start? I have Visual Studio to use as an IDE. Where can I get Android SDK etc? Any help would be appreciated. Thanks. OCO

Leave a Reply


6 × six =

Proudly designed by Theme Junkie.