
Android Send Email from Activity
Intents are messenger objects that are used to call Activities from a particular activity class. There are two types of Intents that you must know Explicit and Implicit Intents.
Implicit intents are used to call Activities that are defined within your project folder. To call other App Activity we need to use Explicit intents.
The code to send an email through your Android application from a Activity class is pretty simple. When this block of code is executed, a Dialog box is displayed with all the Email clients that are available on the device (e.g. Gmail, Mail, Outlook, Hotmail, Yahoomail etc), If no email client is found that you may get a pop-up saying "No Application can perform this action".
Steps:
- Create an Intent object emailIntent with Intent.ACTION_SEND
- Set type as message/rfc822 i.e. for MIME type
- Use Intent.EXTRA_EMAIL as a key to putExtra and where you need to add the Email-ID of the recipient.
- Use Intent.EXTRA_SUBJECT key and pass the value as the Subject of Email.
- Similarly Intent.EXTRA_TEXT value is the Body of the Email.
- In a try catch block call startActivity()
- In the Catch block if ActivityNotFoundException has occurred, i.e No Email client is found, we display a Toast Message.
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(Intent.EXTRA_EMAIL , ""); // email id can be hardcoded too
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello");
emailIntent.putExtra(Intent.EXTRA_TEXT , "This is the Body!");
try {
startActivity(Intent.createChooser(emailIntent, "Done!"));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "No Email client found!!",
Toast.LENGTH_SHORT).show();
}
package com.code2care.example.emailexample;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private EditText emailID,emailSubject,emailBody;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailID = (EditText) findViewById(R.id.emailID);
emailSubject = (EditText) findViewById(R.id.emailSubject);
emailBody = (EditText) findViewById(R.id.emailBody);
}
public void sendEmail(View view) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(Intent.EXTRA_EMAIL , emailID.getText().toString());
emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject.getText().toString());
emailIntent.putExtra(Intent.EXTRA_TEXT , emailBody.getText().toString());
try {
startActivity(Intent.createChooser(emailIntent, "Select a Email Client"));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "No Email client found!!",
Toast.LENGTH_SHORT).show();
}
}
}
Layout.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.code2care.example.toastasservice.ToastasService" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
More Posts related to Android,
- Change Android Toast background color
- Maven : java.lang.ClassNotFoundException: Xmx512m
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Android Alert Dialog with Checkboxes example
- Android Error Generating Final Archive - Debug Certificate Expired
- How to add Newline to text in Android TextView
- Read Text file from SD Card : Android Programming
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- ActivityManager Warning: Activity not started, its current task has been brought to the front
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Android Developers Bluetooth Tutorial
- java.lang.ClassNotFoundException android.support.v7.widget.Toolbar [Fix]
- Android: Save Data in local Db using Android Room
- Channel 50 SMSes received every few minutes Android Phones
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Changing Android Intent Tittle using java code
- Android : No Launcher activity found! Error
- How to change TextView or EditText Text Color on Focus and on Press
- How to display Toast on Button Click : Android
- Android : Execute some code after back button is pressed
- Stop android adb service from command prompt or terminal
- [Soluiton] You already have the latest version of Android Studio installed
- Create Custom Android AlertDialog
- Android R Cannot Be Resolved To A Variable
- How to make Android EditText not editable
More Posts:
- Share Multiple Images in WhatsApp using Android Intent - WhatsApp
- 🎃 Trending, Popular Halloween hashtags for year 2020 🎃 [Facebook, Twitter, Instagram, Snapchat] - Hashtags
- Chrome : When Adobe flash player has finished updating, reload this page to active it - Chrome
- How to recover SharePoint FARM if the SQL Server IP changes - SharePoint
- SharePoint Excel error - The workbook cannot be opened because it contains the following features that are not supported by Excel in the browser - SharePoint
- error CAML Query containing special characters - SharePoint
- 4 Open Source SQLite Editor for Mac OS X , Windows and Linux - Mac-OS-X
- Android Emulator Screenshot saved location - Android-Studio