Saturday, September 8, 2012

How to send an Email From Android

This is a sample activity which shows How to compose a mail and send it with the default email configured on device. 
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 = newIntent(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 = newIntent(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:

No comments:

Post a Comment