android-logo

Sending Email in Android


The default Email app from Android can be used as an email client, so you dont need to develop one from the start. To accomplish this, we need to create an Activity that launches an email client with an implicit Intent and the right action.

Syntax


Intent email = new Intent(Intent.ACTION_SEND);
email.setData(Uri.parse("mailto:"));
email.setType("message/rfc822");
email.putExtra(Intent.EXTRA_EMAIL, "example@gmail.com");
email.putExtra(Intent.EXTRA_CC, "example_cc@gmail.com");
email.putExtra(Intent.EXTRA_SUBJECT, "Add Subject Here");
email.putExtra(Intent.EXTRA_TEXT, "Message Body"); 


How to send email in android using intent

MainActivity.java


package com.androidaura.email;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                writeEmail();
            }
        });
    }

    protected void writeEmail() {

        String[] TO = {""};
        String[] CC = {""};

        Intent email = new Intent(Intent.ACTION_SEND);
        email.setData(Uri.parse("mailto:"));
        email.setType("message/rfc822");
        email.putExtra(Intent.EXTRA_EMAIL, TO);
        email.putExtra(Intent.EXTRA_CC, CC);
        email.putExtra(Intent.EXTRA_SUBJECT, "Add Subject Here");
        email.putExtra(Intent.EXTRA_TEXT, "Message Body");

        try {
            startActivity(Intent.createChooser(email, "Select email client..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
        }
    }
} 

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:p="Android Aura"
        android:textColor="@color/colorAccent"
        android:textSize="25sp"
        android:textStyle="bold"
        android:gravity="center" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout> 

Output
send-email-1

previous-button
next-button

facebook-logo
twitter-logo
instagram-logo

2020 © All rights reserved.