
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>
- Android Error Unexpected cast to Button: layout tag was FrameLayout
- ADT quit unexpectedly error on Mac OSX Android Eclipse SDK
- Parsing Data for android-21 failed unsupported major.minor version 51.0
- Android Studio Ctrl Shift o auto import not working
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
- Android : How to make TextView Scrollable
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Integrating Android Facebook SDK 3.17.2 Tutorial
- Android R Cannot Be Resolved To A Variable
- Android : Exception raised during rendering: action_bar API 22
- How to take screenshot on Android
- Read Text file from SD Card : Android Programming
- How to make Android EditText not editable
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later.
- The declared package does not match the expected package Eclipse
- Can't Run SDK Manager find_java.bat issue
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- Android Emulator Soft Back button action using Computer keyboard
- Multiline EditText in Android Example
- Use 5G Network on Android Emulator
- Make Android TextView Clickable like Buttons
- How to empty trash in Android Device
- Android : Execute some code after back button is pressed
- Disable Fading Edges Scroll Effect Android Views
- How To Disable Landscape Mode in Android Application
- Install Microsoft Remote Desktop (RDP) Client on Mac - Microsoft
- PowerShell iterate and get all SharePoint sites and sub sites - SharePoint
- 🌗 List of Solar Eclipses in the year 2020-2021 🌑 - Science
- Java 8 JDBC: Insert Timestamp Code Example - Java
- MySQL ERROR 1064 (42000): You have an error in your SQL syntax [fix] - MySQL
- How to use Content Assist in Eclipse IDE - Eclipse
- Fetch More then 10 Links Per Page in Google Search Result - Google
- Java monitoring and management console [jconsole] - Java