4 April 2016

Make Android app to send email

What we will do ?
-> We will ask user to input recipient email id,subject and body of the email, then we will send the mail through a email service present in the mobile.

What we will need ?
  • Sample email-id, subject and message for the email
  • 3 EditText and 1 Button
  • Additions in MainActivity.java
Step 1 : Create a new application. Go to File->New->Android Application Project.
file new android app

Step 2 : Give a Name as SendEmail and package name as com.mia.sendemail to your application.Click Next continuously.
sendemail mia
Click Finish.
click finish

Step 3 : Drag 3 EditText and a Button from the Palette to activity_main layout.The code is shown below.
drag 3 edittext and 1 button
activity_main.xml
<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="com.mia.sendemail.MainActivity" >

    <Button
        android:id="@+id/sendbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/body"
        android:layout_marginTop="22dp"
        android:text="Send" />

    <EditText
        android:id="@+id/subject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/inputemail"
        android:ems="10" />

    <EditText
        android:id="@+id/body"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/subject"
        android:ems="10"
        android:inputType="textMultiLine" />

    <EditText
        android:id="@+id/inputemail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/subject"
        android:layout_alignParentTop="true"
        android:layout_marginTop="64dp"
        android:ems="10"
        android:inputType="textEmailAddress" />

</RelativeLayout>

Step 4 : Open MainActivity.java from src->com.mia.sendmail and copy the code below in it.

MainActivity.java
package com.mia.sendemail;

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 MainActivity extends Activity {

 EditText emailid,subject,body;
 Button sendbutton;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  emailid=(EditText) findViewById(R.id.inputemail);
  subject=(EditText) findViewById(R.id.subject);
  body=(EditText) findViewById(R.id.body);
  sendbutton=(Button) findViewById(R.id.sendbutton);
  
  sendbutton.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {   
    String contentemail = emailid.getText().toString();
      String contentsubject = subject.getText().toString();
      String contentbody = body.getText().toString();
  
      Intent email = new Intent(Intent.ACTION_SEND);
      email.putExtra(Intent.EXTRA_EMAIL, new String[]{ contentemail});
      email.putExtra(Intent.EXTRA_SUBJECT, contentsubject);
      email.putExtra(Intent.EXTRA_TEXT, contentbody);
      email.setType("message/rfc822");    
      startActivity(Intent.createChooser(email, "Choose an Email client :"));
   }}); 
 }
}

Step 5 : Run the Application
output email
output email
Explanation of the code :

Explanation from activity_main.xml :
  • We have used 3 EditText for entering email-id, subject and message of the mail.
<EditText
        android:id="@+id/subject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/inputemail"
        android:ems="10" />

    <EditText
        android:id="@+id/body"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/subject"
        android:ems="10"
        android:inputType="textMultiLine" />

    <EditText
        android:id="@+id/inputemail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/subject"
        android:layout_alignParentTop="true"
        android:layout_marginTop="64dp"
        android:ems="10"
        android:inputType="textEmailAddress" />
  • Then we use a Button to send the e-mail.
<Button
        android:id="@+id/sendbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/body"
        android:layout_marginTop="22dp"
        android:text="Send" />
Explanation from MainActivity.java :
  • We assign the EditTexts and Button to their respective variables.
emailid=(EditText) findViewById(R.id.inputemail);
subject=(EditText) findViewById(R.id.subject);
body=(EditText) findViewById(R.id.body);
sendbutton=(Button) findViewById(R.id.sendbutton);
  • Under the onClick() method we convert the input from the EditTexts to String using toString() method.
String contentemail = emailid.getText().toString();
String contentsubject = subject.getText().toString();
String contentbody = body.getText().toString();
  • Then we prepare an Intent named email of ACTION_SEND is used to perform action of sending some data by using this Intent.
Intent email = new Intent(Intent.ACTION_SEND);
  • putExtra() method is used to add extra data to the Intent. We add the email-id,, subject and message to the Intent by using its respective methods. There is also a provision to add BCC to the mail.
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ contentemail});
email.putExtra(Intent.EXTRA_SUBJECT, contentsubject);
email.putExtra(Intent.EXTRA_TEXT, contentbody);
email.setType("message/rfc822");
  • setType() method is used to set the client type.[RFC822]
email.setType("message/rfc822");
  • Then we start the activity using startActivity(). "Choose an Email client" message is used to choose specific email client from those available in the mobile phone.
startActivity(Intent.createChooser(email, "Choose an Email client :"));
Github download
Stay Tuned with Made In Android

Previous Page Next Page Home
Top