Send E-Mail Android Application Tutorial

Introduction

 
Hello all! In this article, we will learn how to create an Android app that will send an email using any email client. In this tutorial, we are using the intent service of Android which will help us to send the email. The intent is very useful in Android development. It helps us to call the needed services, like calling, SMS, GPS, etc. It has a wider role in Android. 
 
Requirements
  • Android Studio 2.3.3
  • Little knowledge of Java and XML
  • Android Phone to test the Android app
Let’s get started.
 
Let’s start creating the Android app using Android Studio. I have also given the download link of the project, that you can find below.
 
Step 1 - Creating a new project with Android studio
 
Open Android Studio and create a new project, name it as "Send Email" and give a company domain whatever you like; for eg: foo.android (You can use your own name also).
  • Click "Next" and choose Min SDK; I have chosen Android 4.1 (Jelly Bean).
  • Click "Next" and select "Empty Activity".
  • Choose the Activity as Main Activity and click "Next", then "Finish".
     
    Android
     
    Android
Once we create our new project, Gradle will take some time to sync the project and will resolve all the dependencies. Sometimes, this process also takes a lot of time.
 
Step 2 - Creating Layout of Send Email App
 
Here, we will create a layout for our app. So, for tutorial purposes, I am keeping it simple. For sending an email, we usually need three fields, i.e., To, Subject, and Message. So, we will be creating a layout for three fields with the help of TextView and EditText and finally, we need a Send button to send the email.
 
The XML code for our layout is shown below.
 
activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3. xmlns:tools="http://schemas.android.com/tools"    
  4. tools:context="in.amitsin6h.sendemail.MainActivity"    
  5. android:layout_width="match_parent"    
  6. android:layout_height="match_parent"    
  7. android:paddingLeft="20px"    
  8. android:paddingRight="20px"    
  9. android:orientation="horizontal"    
  10. >    
  11.         
  12. <EditText    
  13.     android:id="@+id/etTo"    
  14.     android:layout_width="wrap_content"    
  15.     android:layout_height="wrap_content"    
  16.     android:layout_alignParentRight="true"    
  17.     android:layout_alignParentTop="true"    
  18.     android:layout_marginRight="22dp"    
  19.     android:layout_marginTop="16dp"    
  20.     android:ems="10" />    
  21.         
  22. <EditText    
  23.    android:id="@+id/etSub"    
  24.    android:layout_width="wrap_content"    
  25.    android:layout_height="wrap_content"    
  26.    android:layout_alignLeft="@+id/etTo"    
  27.    android:layout_below="@+id/etTo"    
  28.    android:layout_marginTop="18dp"    
  29.    android:ems="10" >    
  30. </EditText>    
  31.       
  32. <EditText    
  33.     android:id="@+id/etMsg"    
  34.     android:layout_width="wrap_content"    
  35.     android:layout_height="wrap_content"    
  36.     android:layout_alignLeft="@+id/etSub"    
  37.     android:layout_below="@+id/etSub"    
  38.     android:layout_marginTop="28dp"    
  39.     android:ems="10"    
  40.     android:inputType="textMultiLine" />    
  41.         
  42. <TextView    
  43.     android:id="@+id/textView1"    
  44.     android:layout_width="wrap_content"    
  45.     android:layout_height="wrap_content"    
  46.     android:layout_alignBaseline="@+id/etTo"    
  47.     android:layout_alignBottom="@+id/etTo"    
  48.     android:layout_alignParentLeft="true"    
  49.     android:text="To:" />    
  50.         
  51. <TextView    
  52.     android:id="@+id/textView2"    
  53.     android:layout_width="wrap_content"    
  54.     android:layout_height="wrap_content"    
  55.     android:layout_alignBaseline="@+id/etSub"    
  56.     android:layout_alignBottom="@+id/etSub"    
  57.     android:layout_alignParentLeft="true"    
  58.     android:text="Subject:" />    
  59.         
  60. <TextView    
  61.     android:id="@+id/textView3"    
  62.     android:layout_width="wrap_content"    
  63.     android:layout_height="wrap_content"    
  64.     android:layout_alignBaseline="@+id/etMsg"    
  65.     android:layout_alignBottom="@+id/etMsg"    
  66.     android:layout_alignParentLeft="true"    
  67.     android:text="Message:" />    
  68.         
  69. <Button    
  70.     android:id="@+id/btSend"    
  71.     android:layout_width="wrap_content"    
  72.     android:layout_height="wrap_content"    
  73.     android:layout_alignLeft="@+id/etMsg"    
  74.     android:layout_below="@+id/etMsg"    
  75.     android:layout_marginLeft="76dp"    
  76.     android:layout_marginTop="20dp"    
  77.     android:text="Send" />    
  78.         
  79. </RelativeLayout>   
    Step 3 – Send Email  Android App Java Code
     
    This is our main part where we will write the code for our app.  We discussed in the introduction that we will be using intent to send an email. Intent uses the email service which will call the email client and send our app data from string to the email client and email client will use that data to send the email. It’s simple and easy to do.  :)
     
    Just copy the below java code and paste it to MainActivity.java.
     
    MainActivity.java
    1. package in.amitsin6h.sendemail;  
    2.   
    3. import android.content.Intent;  
    4. import android.support.v7.app.AppCompatActivity;  
    5. import android.os.Bundle;  
    6. import android.view.View;  
    7. import android.widget.Button;  
    8. import android.widget.EditText;  
    9.   
    10. public class MainActivity extends AppCompatActivity {  
    11.   
    12.     EditText etTo, etSub, etMsg;  
    13.     Button btSend;  
    14.     String to, subject, message;  
    15.   
    16.     @Override  
    17.     protected void onCreate(Bundle savedInstanceState) {  
    18.         super.onCreate(savedInstanceState);  
    19.         setContentView(R.layout.activity_main);  
    20.   
    21.         etTo = (EditText) findViewById(R.id.etTo);  
    22.         etSub = (EditText) findViewById(R.id.etSub);  
    23.         etMsg = (EditText) findViewById(R.id.etMsg);  
    24.   
    25.         btSend = (Button) findViewById(R.id.btSend);  
    26.   
    27.         btSend.setOnClickListener(new View.OnClickListener() {  
    28.             @Override  
    29.             public void onClick(View view) {  
    30.                 to = etTo.getText().toString();  
    31.                 subject = etSub.getText().toString();  
    32.                 message = etMsg.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 Email client :"));  
    44.             }  
    45.         });  
    46.     }  
    47. }  
    If anyone faces a problem in understanding the java code, they  can comment below and I will help you guys to understand.
     
    Step 4 - Compile and Run
     
    Now, we are ready to compile and run our Send Email Android app. The following screen will appear once our app gets installed.
     
    Now, let us write and test the email and press the Send button to check whether it works or not. So, once we click the Send button, it will ask to choose the email client and then choose your email client and after sending the email check your inbox.
     
    Android
     
    Android
     
    Android
     
    Android
     
    Project Source Code


    Similar Articles