Send To Email Apps In Android Application Using Android Studio

Introduction

In this article you will learn how to develop email applications for sending information from one mail to another in Android applications using Android Studio.

Requirements

If you want to develop the email application it should follow the below steps.

Step 1

Now, open Android studio and go to File and New and choose NewProject .



Step 2

Her, you will write the application name and click the next button.

application name

Now, we can choose the application version, it is Target version and devices.

application name

Step 3

Now choose to Activity (Blank Activity, Empty Activity) and click the Next button.

application name

Here, you will write the Activity name and click the Finish button.

application name

Step 4

Here, NewProject is open and you will go to design page (activity_main.xml) and you will build the design, it should follow the drag and drop method, it is the   option (editText,Button).

application name

Here,you will see the design page(Graphical user Interface).

application name

Now,you will see the activity_main.xml code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="xyz.rvconstructions.www.sendemailapp.MainActivity">  
  3.     <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:ems="10" android:id="@+id/editText" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" />  
  4.     <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:ems="10" android:id="@+id/editText2" android:layout_below="@+id/editText" android:layout_alignRight="@+id/editText" android:layout_alignEnd="@+id/editText" />  
  5.     <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:ems="10" android:id="@+id/editText3" android:layout_below="@+id/editText2" android:layout_alignRight="@+id/editText2" android:layout_alignEnd="@+id/editText2" />  
  6.     <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SEND MAIL" android:id="@+id/sendbttn" android:layout_centerVertical="true" android:layout_alignLeft="@+id/editText3" android:layout_alignStart="@+id/editText3" />  
  7.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Recipient" android:id="@+id/textView" android:layout_alignBottom="@+id/editText" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />  
  8.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="subject" android:id="@+id/textView2" android:layout_alignBottom="@+id/editText2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />  
  9.     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Message Body" android:id="@+id/textView3" android:layout_alignBottom="@+id/editText3" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>  
Step 5

Now ,you will build on AndroidManifest.xml code.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xyz.rvconstructions.www.sendemailapp">  
  3.     <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">  
  4.         <activity android:name=".MainActivity">  
  5.             <intent-filter>  
  6.                 <action android:name="android.intent.action.MAIN" />  
  7.                 <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>  
  8.         </activity>  
  9.     </application>  
  10. </manifest>  
Step 6

Now, you will go to MainActivity.java page and you will build the java code.

First of all you will declare the Header file.

application name
  1. package xyz.rvconstructions.www.sendemailapp;  
  2. import android.support.v7.app.AppCompatActivity;  
  3. import android.os.Bundle;  
  4. import android.net.Uri;  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.util.Log;  
  8. import android.view.Menu;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.Toast;  
Now, you will declare and get the reference in the button and perform the OnClickListener.


  1. Button startBtn = (Button) findViewById(R.id.sendbttn);  
  2. startBtn.setOnClickListener(new View.OnClickListener() {  
  3.     public void onClick(View view) {  
  4.         sendEmail();  
  5.     }  
  6. });  
Here, you will assign the method() of sendEmail().

application name
  1. protected void sendEmail() {  
  2.         Log.i("Send email""");  
  3.         String[] TO = {  
  4.             "[email protected]"  
  5.         };  
  6.         String[] CC = {  
  7.             "[email protected]"  
  8.         };  
  9.         Intent emailIntent = new Intent(Intent.ACTION_SEND);  
  10.         emailIntent.setData(Uri.parse("mailto:"));  
  11.         emailIntent.setType("text/plain");  
  12.         emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);  
  13.         emailIntent.putExtra(Intent.EXTRA_CC, CC);  
  14.         emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");  
  15.         emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");  
  16.         try {  
  17.             startActivity(Intent.createChooser(emailIntent, "Send mail..."));  
  18.             finish();  
  19.             Log.i("Finished sending email...""");  
  20.         } catch (android.content.ActivityNotFoundException ex) {  
  21.             Toast.makeText(MainActivity.this"There is no email client installed.", Toast.LENGTH_SHORT).show();  
  22.         }  
  23.     }  
  24.     // This is just a sample script. Paste your real code (javascript or HTML) here.  
  25. if ('this_is' == /an_example/) {  
  26.     of_beautifier();  
  27. else {  
  28.     var a = b ? (c % d) : e[f];  
  29. }  
Step 7

Now ,you will run the application in emulator or devices.

application name

Here,you will choose the emulator or devices

application name

Step 8

Now, you will see the output.

application name

Here, you will fill the data and you will click the SEND EMAIL button.

application name

Now, you will choose one option  --  Gmail.com(Email).

application name

Here,you will see the To and From mail address and click the send option.

application name

Now,you will see that mail send item.

application name

Here,you will recipeint([email protected]) receive the inbox mail.

application name

Summary

Hence, it is used to how to send the mail in one mail to another mail in Android applications using Android Studio.