All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Send Email In Android Programmatically

Last Updated : Mar 11, 2024

How To Send Email In Android Programmatically

In this article we will show you the solution of how to send email in android programmatically, you can send emails from your Android application by using an Android application.

You may accomplish this using Intent with the action set to ACTION SEND and additional fields: the subject, body, and email id of the recipient to whom you wish to send the message.

Intent, which is utilized in this case to send the email, is essentially a straightforward message object that is used to interact across various Android components, including activities,content providers,broadcast receivers, or services.

This application essentially consists of one activity with an EditText to allow the user to input their email address, topic, and body, as well as a button to send the email.

Step By Step Guide On How To Send Email In Android Programmatically :-

XML

<?xml version="1.0" encoding="utf-8"?>
<!-- Relative Layout -->
<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"
    tools:context=".MainActivity">
    <!-- Edit text for email id -->
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="18dp"
        android:layout_marginRight="22dp" />
    <!-- Edit text for email subject -->
    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_alignLeft="@+id/editText1"
        android:layout_marginTop="20dp" />
    <!-- Edit text for email body -->
    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_alignLeft="@+id/editText2"
        android:layout_marginTop="30dp" />
    <!-- text Views for label -->
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:text="Send To:"
        android:textColor="#0F9D58" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText2"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignParentLeft="true"
        android:text="Email Subject:"
        android:textColor="#0F9D58" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText3"
        android:layout_alignBottom="@+id/editText3"
        android:text="Email Body:"
        android:textColor="#0F9D58" />
    <!-- Button to send email -->
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText3"
        android:layout_alignLeft="@+id/editText3"
        android:layout_marginLeft="76dp"
        android:layout_marginTop="20dp"
        android:text="Send email!!" />
</RelativeLayout>

Java

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    // define objects for edit text and button
    Button button;
    EditText sendto, subject, body;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        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(view -> {
            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 three fields to intent using putExtra function
            intent.putExtra(Intent.EXTRA_EMAIL, new String[]{emailsend});
            intent.putExtra(Intent.EXTRA_SUBJECT, emailsubject);
            intent.putExtra(Intent.EXTRA_TEXT, emailbody);
            // set type of intent
            intent.setType("message/rfc822");
            // startActivity with intent with chooser as Email client using createChooser function
            startActivity(Intent.createChooser(intent, "Choose an Email client :"));
        });
    }
}
  1. Start a new project in Android Studio by opening it. Run code to start a new project using Android Studio. Both Java and the Android programming language Kotlin have been provided with the code for it.
  2. Using the XML Files. Next, navigate to the activity main.xml file, which is the project's Interface. The code again for activity main.xml file is shown below. Inside the code, comments are added to help the reader comprehend it better.
  3. This file includes a Relative Layout with three Editing texts for the recipient's email address, another for the email's subject, and a final one for the body, along with three TextViews for such label and just a button for launching the intent or sending the email.
  4. Modifying the MainActivity File. The following code can be found in the MainActivity File. The MainActivity File's source code is provided below. Inside the code, comments are added to help the reader comprehend it better.
  5. The putExtra function is used to add three additional fields to the intent object, which is generated in the MainActivity and has the action defined as ACTION SEND for sending an email.
  6. These are the fields: recipient's email, Topic of email. In order to create an intent with the action of sending an email and the intent type indicated in the code, the body of the email is attached to the a button with intent object.

Conclusion :-

Before beginning an email activity, you should be aware of the capabilities of email with intent.

Data transmission from one component to another,whether inside and outside of an application, is known as intent.

You can utilize an existing email client, such as Android default Email Apps,Gmail,Outlook, K-9 Mail, etc. and send emails from our application without having to create one from scratch.

I hope this article on how to send email in android programmatically helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪