Send email in android

In Android, the Intent is used to send Email.

Code: Send Email in android:

Intent email = new Intent(Intent.ACTION_SEND);  
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});  
email.putExtra(Intent.EXTRA_SUBJECT, subject);  
email.putExtra(Intent.EXTRA_TEXT, message);  
 
//need this to prompts email client only  
email.setType("message/rfc822");  
 
startActivity(Intent.createChooser(email, "Choose an Email client :"));

Example:

In the below example, we are using an Intent object to send an email from our app. It launches the existing email clients.

activity_main.xml:

In the activity_main.xml file, we will drag two EditTexts, a MultiLine EditText, three TextViews, and a Button from the palette.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="30dp" />
 
 
 
    <Button
        android:id="@+id/sendEmail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/compose_email"/>
 
</LinearLayout>

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we will write the code to send an email via intent.

package com.example.radioapp;
 
import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity {
    @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();
            }
        });
    }
 
    @SuppressLint("LongLogTag")
    protected void sendEmail() {
        Log.i("Send email", "");
        String[] TO = {""};
        String[] CC = {""};
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
 
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("text/plain");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
        emailIntent.putExtra(Intent.EXTRA_CC, CC);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        emailIntent.putExtra(Intent.EXTRA_TEXT, "Write your Email message here");
 
        try {
            startActivity(Intent.createChooser(emailIntent, "Send"));
            finish();
            Log.i("Message Sent!!", "");
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MainActivity.this, "No email client installed.", Toast.LENGTH_SHORT).show();
        }
    }
}

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.radioapp" >
 
        <application
            android:allowBackup="true"
            android:icon="@drawable/php"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
 
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
 
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
 
            </activity>
 
        </application>
    </manifest>

Strings.xml:

<resources>
    <string name="app_name">radioapp</string>
    <string name="compose_email">Compose Email</string>
    <!-- TODO: Remove or change this placeholder text -->
 
</resources>

Output 1:

Output 2:

Please follow and like us:
Content Protection by DMCA.com