Send Email

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

How to send email in android using intent

We can easily send email in android via intent. You need to write few lines of code only as given below

  1. Intent email = new Intent(Intent.ACTION_SEND);  

  2. email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});  

  3. email.putExtra(Intent.EXTRA_SUBJECT, subject);  

  4. email.putExtra(Intent.EXTRA_TEXT, message);  

  5.        

  6. //need this to prompts email client only  

  7. email.setType("message/rfc822");  

  8.   

  9. startActivity(Intent.createChooser(email, "Choose an Email client :"));  

Example of phone call in android

activity_main.xml

Drag the 2 EditTexts, 1 MultiLine EditText, 3 TextViews and 1 Button from the pallete, now the activity_main.xml file will like this:

File: activity_main.xml
 
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/editText1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentRight="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginRight="22dp"  
  14.         android:layout_marginTop="16dp"  
  15.         android:ems="10" />  
  16.   
  17.     <EditText  
  18.         android:id="@+id/editText2"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignLeft="@+id/editText1"  
  22.         android:layout_below="@+id/editText1"  
  23.         android:layout_marginTop="18dp"  
  24.         android:ems="10" >  
  25.   
  26.         <requestFocus />  
  27.     </EditText>  
  28.   
  29.     <EditText  
  30.         android:id="@+id/editText3"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.         android:layout_alignLeft="@+id/editText2"  
  34.         android:layout_below="@+id/editText2"  
  35.         android:layout_marginTop="28dp"  
  36.         android:ems="10"  
  37.         android:inputType="textMultiLine" />  
  38.   
  39.     <TextView  
  40.         android:id="@+id/textView1"  
  41.         android:layout_width="wrap_content"  
  42.         android:layout_height="wrap_content"  
  43.         android:layout_alignBaseline="@+id/editText1"  
  44.         android:layout_alignBottom="@+id/editText1"  
  45.         android:layout_alignParentLeft="true"  
  46.         android:text="To:" />  
  47.   
  48.     <TextView  
  49.         android:id="@+id/textView2"  
  50.         android:layout_width="wrap_content"  
  51.         android:layout_height="wrap_content"  
  52.         android:layout_alignBaseline="@+id/editText2"  
  53.         android:layout_alignBottom="@+id/editText2"  
  54.         android:layout_alignParentLeft="true"  
  55.         android:text="Subject:" />  
  56.   
  57.     <TextView  
  58.         android:id="@+id/textView3"  
  59.         android:layout_width="wrap_content"  
  60.         android:layout_height="wrap_content"  
  61.         android:layout_alignBaseline="@+id/editText3"  
  62.         android:layout_alignBottom="@+id/editText3"  
  63.         android:layout_alignParentLeft="true"  
  64.         android:text="Message:" />  
  65.   
  66.     <Button  
  67.         android:id="@+id/button1"  
  68.         android:layout_width="wrap_content"  
  69.         android:layout_height="wrap_content"  
  70.         android:layout_alignLeft="@+id/editText3"  
  71.         android:layout_below="@+id/editText3"  
  72.         android:layout_marginLeft="76dp"  
  73.         android:layout_marginTop="20dp"  
  74.         android:text="Send" />  
  75.   
  76. </RelativeLayout>  

Activity class

Let's write the code to send email via intent.

File: MainActivity.java
 
  1. package com.example.sendemail;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     EditText editTextTo,editTextSubject,editTextMessage;  
  14.     Button send;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.           
  20.         editTextTo=(EditText)findViewById(R.id.editText1);  
  21.         editTextSubject=(EditText)findViewById(R.id.editText2);  
  22.         editTextMessage=(EditText)findViewById(R.id.editText3);  
  23.           
  24.         send=(Button)findViewById(R.id.button1);  
  25.           
  26.         send.setOnClickListener(new OnClickListener(){  
  27.   
  28.             @Override  
  29.             public void onClick(View arg0) {  
  30.                  String to=editTextTo.getText().toString();  
  31.                  String subject=editTextSubject.getText().toString();  
  32.                  String message=editTextMessage.getText().toString();  
  33.                    
  34.                   
  35.                  Intent email = new Intent(Intent.ACTION_SEND);  
  36.                   email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});  
  37.                   email.putExtra(Intent.EXTRA_SUBJECT, subject);  
  38.                   email.putExtra(Intent.EXTRA_TEXT, message);  
  39.        
  40.                   //need this to prompts email client only  
  41.                   email.setType("message/rfc822");  
  42.        
  43.                   startActivity(Intent.createChooser(email, "Choose an Email client :"));  
  44.        
  45.             }  
  46.               
  47.         });  
  48.     }  
  49.   
  50.     @Override  
  51.     public boolean onCreateOptionsMenu(Menu menu) {  
  52.         // Inflate the menu; this adds items to the action bar if it is present.  
  53.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  54.         return true;  
  55.     }  
  56.   
  57. }