Send Email in Android

Introduction

 
This article explains how to send an Email in Android. Android Studio is used to create the sample.
 
This application will send an email on a button click. In this, you will first create an XML file and use three textViews, three editTexts, and a button. So the first edittext will use the email id of the recipient, the second edittext will use the subject and the third will contain the message. On a button click, you can send the email. We use an intent to send the email so you will get the intent in the intent object. You will call the putExtra() method that es the email id, subject and text.
  1. Intent i=new Intent(Intent.ACTION_SEND);  
  2.                 i.putExtra(Intent.EXTRA_EMAIL,new String[]{to});  
  3.                 i.putExtra(Intent.EXTRA_SUBJECT,subject);  
  4.                 i.putExtra(Intent.EXTRA_TEXT,message);  
  5.                 i.setType("meassge/rfc822");  
  6.                 startActivity(Intent.createChooser(i,"choose:")); 
Use the following procedure to create a sample of sending an Email in Android.
 
Step 1
 
Create an XML file.
 
In the XML file, you will first create an XML file and use three textViews, three editTexts, and a button. So the first edittext will use the email id of the recipient, the second edittext will use the subject and the third will contain the message on a button click.
  1. <RelativeLayout xmlns:android="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.    android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.    android:paddingRight="@dimen/activity_horizontal_margin"  
  7.    android:paddingTop="@dimen/activity_vertical_margin"  
  8.    android:paddingBottom="@dimen/activity_vertical_margin"  
  9.    tools:context=".MainActivity">  
  10.    <LinearLayout  
  11.       android:layout_height="wrap_content"  
  12.       android:layout_width="wrap_content"  
  13.       android:orientation="horizontal"  
  14.       >  
  15.       <TextView  
  16.          android:id="@+id/textView1"  
  17.          android:ems="4"  
  18.          android:textSize="30dp"  
  19.          android:layout_width="wrap_content"  
  20.          android:layout_height="wrap_content"  
  21.          android:text="To" />  
  22.       <EditText  
  23.          android:layout_marginLeft="0dp"  
  24.          android:id="@+id/editText1"  
  25.          android:layout_width="200dp"  
  26.          android:layout_height="wrap_content"  
  27.          />  
  28.    </LinearLayout>  
  29.    <LinearLayout  
  30.       android:layout_height="wrap_content"  
  31.       android:layout_width="wrap_content"  
  32.       android:layout_marginTop="100dp"  
  33.       android:orientation="horizontal">  
  34.       <TextView  
  35.          android:id="@+id/textView1"  
  36.          android:ems="4"  
  37.          android:textSize="30dp"  
  38.          android:layout_width="wrap_content"  
  39.          android:layout_height="wrap_content"  
  40.          android:text="Subject" />  
  41.       <EditText  
  42.          android:layout_marginLeft="0dp"  
  43.          android:id="@+id/editText2"  
  44.          android:layout_width="200dp"  
  45.          android:layout_height="wrap_content"  
  46.          />  
  47.    </LinearLayout>  
  48.    <LinearLayout  
  49.       android:layout_height="wrap_content"  
  50.       android:layout_width="wrap_content"  
  51.       android:orientation="horizontal"  
  52.       android:layout_marginTop="200dp">  
  53.       <TextView  
  54.          android:id="@+id/textView1"  
  55.          android:ems="4"  
  56.          android:textSize="30dp"  
  57.          android:layout_width="wrap_content"  
  58.          android:layout_height="wrap_content"  
  59.          android:text="Message" />  
  60.       <EditText  
  61.          android:layout_marginLeft="0dp"  
  62.          android:id="@+id/editText3"  
  63.          android:layout_width="200dp"  
  64.          android:inputType="textImeMultiLine"  
  65.          android:layout_height="wrap_content"  
  66.          />  
  67.    </LinearLayout>  
  68.    <Button  
  69.       android:id="@+id/buttonSend"  
  70.       android:layout_width="150dp"  
  71.       android:text="Send"  
  72.       android:layout_marginLeft="100dp"  
  73.       android:layout_height="wrap_content"  
  74.       android:layout_marginTop="350dp"></Button>  
  75. </RelativeLayout> 
Step 2
 
Create a Java class file.
 
We use an intent to send an email so you will get the intent in the intent object. You will call the putExtra() method that es the email id, subject and text as in the following:
  1. package com.sendemail;  
  2.    
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10.    
  11. public class MainActivity extends Activity {  
  12.    
  13.     EditText editText1,editText2,editText3;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.    
  19.         editText1=(EditText)findViewById(R.id.editText1);  
  20.    
  21.         editText2=(EditText)findViewById(R.id.editText2);  
  22.         editText3=(EditText)findViewById(R.id.editText3);  
  23.         Button buttonSend=(Button)findViewById(R.id.buttonSend);  
  24.         buttonSend.setOnClickListener(new View.OnClickListener() {  
  25.             @Override  
  26.             public void onClick(View v) {  
  27.    
  28.                 String to=editText1.getText().toString();  
  29.                 String subject=editText2.getText().toString();  
  30.                 String message=editText3.getText().toString();  
  31.    
  32.                 Intent i=new Intent(Intent.ACTION_SEND);  
  33.                 i.putExtra(Intent.EXTRA_EMAIL,new String[]{to});  
  34.                 i.putExtra(Intent.EXTRA_SUBJECT,subject);  
  35.                 i.putExtra(Intent.EXTRA_TEXT,message);  
  36.                 i.setType("meassge/rfc822");  
  37.                 startActivity(Intent.createChooser(i,"choose:"));  
  38.             }  
  39.         });  
  40.    
  41.     }  
  42.     @Override  
  43.     public boolean onCreateOptionsMenu(Menu menu) {  
  44.         // Inflate the menu; this adds items to the action bar if it is present.  
  45.         getMenuInflater().inflate(R.menu.main, menu);  
  46.         return true;  
  47.     }  
Step 3
 
Android Manifest. xml file
 
In the Android Manifest.xml file you will provide the internet permission as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.sendemail"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.sendemail.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26. <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
  27. </manifest> 
Step 4
 
To run this application in the emulator you need to create an id form to send the Email. To create the id you go to the emulator settings and select add account; it will provide you a new page where you will enter your email id and word.
 
Go to the settings of the emulator as in the following:
 
settings1
 
Click on "+Add Account"; it will display a new page where you will enter the email_id from which you will send the email id to the other user.
 
settings2
 
Click on the "Next" button.
 
setting3
 
Step 5
 
After running your application your email will be sent upon button click.


Similar Articles