Wednesday, March 23, 2011

Send email using Intent.ACTION_SEND

A example to send email using Intent.ACTION_SEND.

Send email using Intent.ACTION_SEND

<?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"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Enter email address:"
   />
<EditText 
   android:id="@+id/email_address"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:inputType="textEmailAddress"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Enter email Subject:"
   />
<EditText 
   android:id="@+id/email_subject"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:inputType="textEmailSubject"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Enter Text:"
   />
<EditText 
   android:id="@+id/email_text"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<Button 
   android:id="@+id/sendemail_intent"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text=" Send email using Intent.ACTION_SEND "
   />
</LinearLayout>


package com.exercise.AndroidEMail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidEMail extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
      
       final EditText edittextEmailAddress = (EditText)findViewById(R.id.email_address);
       final EditText edittextEmailSubject = (EditText)findViewById(R.id.email_subject);
       final EditText edittextEmailText = (EditText)findViewById(R.id.email_text);
       Button buttonSendEmail_intent = (Button)findViewById(R.id.sendemail_intent);
      
       buttonSendEmail_intent.setOnClickListener(new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub

   String emailAddress = edittextEmailAddress.getText().toString();
   String emailSubject = edittextEmailSubject.getText().toString();
   String emailText = edittextEmailText.getText().toString();

   String emailAddressList[] = {emailAddress};
  
   Intent intent = new Intent(Intent.ACTION_SEND); 
   intent.setType("plain/text");
   intent.putExtra(Intent.EXTRA_EMAIL, emailAddressList);  
   intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject); 
   intent.putExtra(Intent.EXTRA_TEXT, emailText); 
   startActivity(Intent.createChooser(intent, "Choice App t send email:"));
  
  }});
   }
}


Download the files.

Related article:
- No applications can perform this action - Check Instent available and force to install
- Send email with Image by starting activity using Intent of ACTION_SEND

6 comments:

Karter said...

Very happy I found your site. Will note it and return for more info.

buy cialis

Mohamad Azhar Inamdar said...

Hi,
I tried ur code but i am getting the following dialog box

"No applications can perform this action"

Android Er said...

hello Mohamad Azhar Inamdar,

Do you have any app installed to send email? such as gmail.

In the HELL of Cyber Mania said...

Hi, i am getting the error while run this code. i.e "No applications can perform this action".

please help me out.

Android Er said...

hello Mohamad Azhar Inamdar and In the HELL of Cyber Mania

Please read No applications can perform this action - Check Instent available and force to install.

Anonymous said...

its cause you have used plain/ text as opposed to text/plain