Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How To Send Email in Android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 384
    Comment on it

    Hello Everyone !!

    In Android, Intent.ACTION_SEND can be used to send an email and we do not require to make an email client for it but we can use the existing clients. If there is no client for this action, The system will display the “No application can perform this action” error.

    Before Start the Email Activity, We have to know about the Intent's email activity. Intent is an object which can carry the information within the activity or outside the activity, In Other Words, Intent is the intention to perform the action in android.

     

    Intent Object For Sending the Email:- 

    Intent.ACTION_SEND is used to Launch the installed Email Client.

    Intent email_Intent = new Intent(Intent.ACTION_SEND);

    To Send the email we have to specify mailto

    email_Intent.setData(Uri.parse("mailto:"));
    email_Intent.setType("text/plain");
    

     

    Extra To Send Email:-

    Android has built in supports to add TO, SUBJECT, CC, TEXT etc. Before Launching the email client, We use these fields for sending the email. 

    email_Intent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"Recipient"});
    email_Intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
    email_Intent.putExtra(Intent.EXTRA_TEXT   , "Message Body");
    

    It is the example to use these fields.

    Below is an example to send the email using the intent but it is not working in the emulator because of there is no any pre-installed email client, so we need to use the Android phone which has email client to sent the email.

    XML Code:-

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:background="#456170"
        android:layout_height="match_parent"
        tools:context="com.emailintentdemo.MainActivity">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="368dp"
            android:layout_height="wrap_content"
            android:text="SENDING EMAIL"
            android:textAlignment="center"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:textSize="30dp"
            android:textColor="@android:color/white"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/sendEmail" />
    
        <Button
            android:id="@+id/sendEmail"
            android:layout_width="205dp"
            android:layout_height="wrap_content"
            android:text="Compose_Email"
            tools:layout_editor_absoluteY="44dp"
            tools:layout_editor_absoluteX="81dp" />
    
    
    </android.support.constraint.ConstraintLayout>

    MainActivity Code:-

    package com.emailintentdemo;
    
    import android.content.Intent;
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button startBtn = (Button) findViewById(R.id.sendEmail);
            startBtn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    sendEmail();
                }
            });
        }
    
        private void sendEmail() {
            Log.i("Send email", "");
            Intent email_Intent = new Intent(Intent.ACTION_SEND);
            String[] TO = {""};
            String[] CC = {""};
            email_Intent.setData(Uri.parse("mailto:"));
            email_Intent.setType("text/plain");
            email_Intent.putExtra(Intent.EXTRA_EMAIL, TO);
            email_Intent.putExtra(Intent.EXTRA_CC, CC);
            email_Intent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
            email_Intent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
    
            try {
                startActivity(Intent.createChooser(email_Intent, "Send mail..."));
                finish();
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(MainActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
            }
        }
        }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: