home top ad

How to send email in android programmatically

Hello, Guys Today in this tutorial we will learn how to send email to Android systematically. We frequently use this function in our application, such as how to write direct messages from a chat app and send it to any chat app.
In this application, using Intent.ACTION_SEND, we can send messages by sending messages in Gmail on our own.

Create New Project

First, you need to create a project in your Android Studio.With this, you will first go to the file and go to a new project and then click and create a new project. First we create a project, we will see two files, first activity_main.xml and MainActivity.java

activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Mail Form"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/to"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="10dp"
        android:ems="10"
        android:inputType="textMultiLine" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="48dp"
        android:text="MESSAGE :"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/to"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:text="TOPIC"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <Button
        android:id="@+id/btn_mail"
        style="@style/Animation.AppCompat.Dialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView5"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="86dp"
        android:text="SUMIT" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:text="TO :"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/topic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView5"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:ems="10"
        android:inputType="textMultiLine" />
</RelativeLayout>
In this layout we used RelativeLayout, within which we used three TextView, one button and three EditText In edittext we will take the input from the user and send it with a button.

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn =  findViewById(R.id.btn_mail);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  Message to be sent by mailspan
               EditText message =  findViewById(R.id.message);
                String me = message.getText().toString();
                //Subject to mail
               EditText mail = findViewById(R.id.to);
                String to = mail.getText().toString();
                //  To whom mail will be sent
                EditText topic = findViewById(R.id.topic);
                String top = topic.getText().toString();
                //Lets check
                if (me.isEmpty() && to.isEmpty() && top.isEmpty()) {
                    Toast.makeText(getApplicationContext(), "Fill all field ...", Toast.LENGTH_SHORT).show();
                } else {
                    Intent email = new Intent(Intent.ACTION_SEND);
                    email.setType("message/rfc822");
                    email.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
                    email.putExtra(Intent.EXTRA_SUBJECT, top);
                    email.putExtra(Intent.EXTRA_TEXT, me);
                    //Let's look at an application that will send
                    try {
                        startActivity(Intent.createChooser(email, "Mail with...."));
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(getApplicationContext(), "Don't", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}
In this activity, we will take input from the user via edittext and store it in string variable and after that we will check in an if condition that all three fields are filled or if there is no filling we will use a toast that fills the whole field and After that, in a more advanced case, the objective will fill all the store data with Intent and use a try / catch or the application may be disrupted.

A screenshot is provided below so the app can see how the app will work
I hope you like this article lot
Thanks ...

Post a Comment

0 Comments