Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
How to Send an Email From an Android Application?
Next article icon

How to Send an Email From an Android Application?

Last Updated : 30 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will make a basic Android Application that can be used to send email through your android application. You can do so with the help of Implicit Intent with action as ACTION_SEND with extra fields:

  • email id to which you want to send mail,
  • the subject of the email, and
  • body of the email.

Basically Intent is a simple message object that is used to communicate between android components such as activities, content providers, broadcast receivers, and services, here use to send the email. This application basically contains one activity with EditText to take input of email address, subject, and body of the email from the user and button to send that email.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Working with the XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail. This file contains a Relative Layout which contains three Edit texts for receiver mail id, another for the subject of the mail, and last one for the body of the email, and three TextViews for the label and a button for starting intent or sending mail: 

activity_main.xml:

XML
<?xml version="1.0" encoding="utf-8"?> <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:background="@color/white"     tools:context=".MainActivity">      <EditText         android:id="@+id/editText1"         android:layout_width="200dp"         android:layout_height="wrap_content"         android:layout_alignParentTop="true"         android:layout_alignParentEnd="true"         android:layout_marginTop="100dp"         android:layout_marginEnd="22dp" />      <EditText         android:id="@+id/editText2"         android:layout_width="200dp"         android:layout_height="wrap_content"         android:layout_below="@+id/editText1"         android:layout_alignStart="@+id/editText1"         android:layout_marginTop="20dp" />      <EditText         android:id="@+id/editText3"         android:layout_width="200dp"         android:layout_height="wrap_content"         android:layout_below="@+id/editText2"         android:layout_alignStart="@+id/editText2"         android:layout_marginTop="30dp" />      <TextView         android:id="@+id/textView1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:padding="16dp"         android:textSize="18sp"         android:textStyle="bold"         android:layout_alignBaseline="@+id/editText1"         android:layout_alignBottom="@+id/editText1"         android:layout_alignParentStart="true"         android:text="Send To:"         android:textColor="@color/green" />      <TextView         android:id="@+id/textView2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:padding="16dp"         android:textSize="18sp"         android:textStyle="bold"         android:layout_alignBaseline="@+id/editText2"         android:layout_alignBottom="@+id/editText2"         android:layout_alignParentStart="true"         android:text="Email Subject:"         android:textColor="@color/green" />      <TextView         android:id="@+id/textView3"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:padding="16dp"         android:textSize="18sp"         android:textStyle="bold"         android:layout_alignBaseline="@+id/editText3"         android:layout_alignBottom="@+id/editText3"         android:text="Email Body:"         android:textColor="@color/green" />      <Button         android:id="@+id/button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/editText3"         android:layout_centerHorizontal="true"         android:textSize="24sp"         android:layout_marginTop="20dp"         android:backgroundTint="@color/green"         android:text="Send email!!" /> </RelativeLayout> 

Layout:

Layout_SendEmail


Step 3: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.

In MainActivity Intent object is created and its action is defined to ACTION_SEND to send an email, with Intent three extra fields are also added using the putExtra function. These fields are:

  • Email of receiver
  • Subject of email
  • Body of email

setOnClickListener is attached to a button with the intent object in it to make intent with action as ACTION_SEND to send email and intent type as shown in code.

Here is the complete code to send email through intent from the android application:

Java
package org.geeksforgeeks.demo;  import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText;  import androidx.appcompat.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {      // Declare EditText and Button objects     private Button button;     private EditText sendTo;     private EditText subject;     private EditText body;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Initialize EditText and Button         sendTo = findViewById(R.id.editText1);         subject = findViewById(R.id.editText2);         body = findViewById(R.id.editText3);         button = findViewById(R.id.button);          // Set OnClickListener for the button         button.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 String emailSend = sendTo.getText().toString();                 String emailSubject = subject.getText().toString();                 String emailBody = body.getText().toString();                  // Define Intent object with action attribute as ACTION_SEND                 Intent intent = new Intent(Intent.ACTION_SEND);                  // Add email details using putExtra                 intent.putExtra(Intent.EXTRA_EMAIL, new String[]{emailSend});                 intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);                 intent.putExtra(Intent.EXTRA_TEXT, emailBody);                  // Set the type of intent                 intent.setType("message/rfc822");                  // Start the activity with a chooser for email clients                 startActivity(Intent.createChooser(intent, "Choose an Email client :"));             }         });     } } 
Kotlin
package org.geeksforgeeks.demo import android.content.Intent import android.os.Bundle import android.widget.Button import android.widget.EditText import androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {      // define objects for edit text and button     private lateinit var button: Button     private lateinit var sendTo: EditText     private lateinit var subject: EditText     private lateinit var body: EditText      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          // Getting instance of edittext and button         sendTo = findViewById(R.id.editText1)         subject = findViewById(R.id.editText2)         body = findViewById(R.id.editText3)         button = findViewById(R.id.button)          // attach setOnClickListener to button with Intent object define in it         button.setOnClickListener {             val emailSend = sendTo.text.toString()             val emailSubject = subject.text.toString()             val emailBody = body.text.toString()              // define Intent object with action attribute as ACTION_SEND             val intent = Intent(Intent.ACTION_SEND)              // add three fields to intent using putExtra function             intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(emailSend))             intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject)             intent.putExtra(Intent.EXTRA_TEXT, emailBody)              // set type of intent             intent.type = "message/rfc822"              // startActivity with intent with chooser as Email client using createChooser function             startActivity(Intent.createChooser(intent, "Choose an Email client :"))         }     } } 

Output:


Next Article
How to Send an Email From an Android Application?

R

Rishabh007
Improve
Article Tags :
  • Android
  • Kotlin Android
  • Java-Android

Similar Reads

    Send Email in an Android Application using Jetpack Compose
    Many times while building an android application. We have to send a mail to any specific address from our android application. Inside this mail, we have to define the email address, subject, and body to be sent over Gmail or any other mail application. In this article, we will be building a simple a
    7 min read
    How to Update/Change Email from Firebase in Android?
    In any app, we got a feature to login using our email and password. Sometimes it happens that we want to change our email or we lost the password of our previous email so here we are going to implement the same feature to change our email using Firebase Authentication. Note that we are going to impl
    3 min read
    How to Share a Captured Image to Another Application in Android?
    Pre-requisite: How to open a Camera through Intent and capture an image In this article, we will try to send the captured image (from this article) to other apps using Android Studio. Approach: The image captured gets stored on the external storage. Hence we need to request permission to access the
    6 min read
    How to Setup Firebase Email Authentication in Android with Example
    Service-access permissions are configured using a mechanism called Firebase authentication. This is accomplished by using Google's Firebase APIs and Firebase console to create and manage legitimate user accounts. You'll discover how to integrate Firebase authentication into an Android application in
    4 min read
    How to Send Email in Android App Without Using Intent?
    It is crucial to establish an email connection in the Android application. This enables us to send important messages to users. For example, when the user joins the app first time sending a welcome email is a common practice. Additionally in some applications if a user signs in from a new location a
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences