How to send email from and ANDROID Application programatically?

By | May 16, 2011

Hello all……..
In today’s post I will show you send mail from an android application progrmatically..
Let’s go to the code fast……
This is the code in the mail java file….

package com.coderzheaven;

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;
import android.widget.Toast;

public class sendMailDemo extends Activity {
    Button send;
    EditText address, subject, emailbody;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        address = (EditText) findViewById(R.id.address);
        subject = (EditText) findViewById(R.id.subject);
        emailbody = (EditText) findViewById(R.id.body);
        send = (Button) findViewById(R.id.send);

        send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	sendEmail();
            }
        });
    }

    public void sendEmail(){

    	if(!address.getText().toString().trim().equalsIgnoreCase("")){
    	  final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
	      emailIntent.setType("plain/text");
	      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ address.getText().toString()});
	      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());
	      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailbody.getText());
	      sendMailDemo.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
	    }
    	else{
    		Toast.makeText(getApplicationContext(), "Please enter an email address..", Toast.LENGTH_LONG).show();
    	}
      }
	}

Now the layout file (main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:background="@drawable/android"
	>
	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:id="@+id/emailaddress"
		android:text="Email Address"
		android:textStyle="bold">
	</TextView>

	<EditText
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:width="250dip"
		android:hint="email address"
		android:id="@+id/address">
	</EditText>

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Subject"
		android:textStyle="bold">
	</TextView>

	<EditText
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:width="250dip"
		android:hint="Subject"
		android:id="@+id/subject">
	</EditText>

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Your Message"
		android:textStyle="bold">
	</TextView>
	<EditText
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:lines="5"
		android:hint="Your message here!!"
		android:id="@+id/body">
	</EditText>
	<Button
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:id="@+id/send"
		android:text="Send Email"
		android:width="150dip">
	</Button>
</LinearLayout>

The AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="Send Mail Demo">
        <activity android:name=".sendMailDemo"
                  android:label="Send Mail Demo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Note: However if you test this in your emulator, it will not work. Install it in your device to test it.

8 thoughts on “How to send email from and ANDROID Application programatically?

  1. dynamic data in email body

    how to send dynamic arraylist string value in email body? for example i want to send a student report which is a dynamic database value transfer from previous activity to email body.How to do this?
    please reply

    Reply
    1. James

      Either read the database and supply that value for the arraylist. or attach the values as extras in the activity and read in the next activity.
      check this post for passing parameters between activities.
      http://coderzheaven.com/2011/03/passing-data-between-intents-in-android/
      In this line
      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailbody.getText());
      you can read value from the arraylist using a for loop or something and supply it as a string in the email body.

      Reply
  2. Aamirkhan

    hi!i am new to android.i have given task to send html email with style of bold,italic,underline,font color,text size, i am able to do bold,italic,underline,font color,with gmail client but for any other client it’s not working.and text size is not working with gmail client as well. can any one help me to solve it Thanks in Advance Aamirkhan I.

    Reply
  3. krishna kumar

    hai i am new in android how to access sent data receive data between android and server using php SOAP( -api)in php

    Reply
  4. Jenny

    hi james in emulator it s showing the page but in mobile v r getting as “java.net.SocketException:The operation timed out”
    after clicking the button…can u pls help…thanks in advance..

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *