Code2care : TechZone Apps & Tutorials

How to send an Email from Android App using Intent

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.

Android Send Email from Activity Screen


Select Email Client


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".



Explanation :



    1. Create an Intent object emailIntent with Intent.ACTION_SEND
    2. Set type as message/rfc822 i.e. for MIME type
    3. Use Intent.EXTRA_EMAIL as a key to putExtra and where you need to add the Email-ID of the recipient.
    4. Use Intent.EXTRA_SUBJECT key and pass the value as the Subject of Email.
    5. Similarly Intent.EXTRA_TEXT value is the Body of the Email.
    6. In a try catch block call startActivity()
    7. 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();
    }



    Email Example App



    Lets create a App that has an Activity with 3 EditTexts Email-Id, Subject and Email Body and a Send Button, When the send button is clicked a pop-up is displayed to choose an Email client, after a client is selected email is being send!.


    MainActivity.java



    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 file



    <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>

Posts related to android.


1. Share image and text Twitter using your Android Application Programatically

2. The Android Virtual Device 'myEmulator' is currently running an emulator and cannot be deleted.

3. Make Android View Scrollable both Horizontally and Vertically

4. Can we move apps like WhatsApp, Facebook to external MicroSD card

5. Parsing Data for android-21 failed unsupported major.minor version 51.0

6. Android : Prevent App for rotation landscape or portrait

7. ADT quit unexpectedly error on Mac OSX Android Eclipse SDK

8. Android-Failed to install apk on device EOF Timeout Error

9. How to change Android EditText Cursor Color

10. JavaDocs for Android SDK Eclipse IDE

11. Two Buttons next to each other in Android Layout

12. Change Title text for Android Activity using java code

13. How to add Newline to text in Android TextView

14. Android : Unable to load VM from snapshot : Mac OS X Error

15. INVALID FILE NAME: MUST CONTAIN ONLY [a-z0-9_.] Android Eclipse Error

16. Hide Tittle bar and Navigation bar from android activity

17. appcompat_v7 errors after updates to API level 21 Material Theme

18. How to Copy Text to Android Clipboard Programatically ClipboardManager (Both Old and New SDK)

19. [Solution] Running Android Lint has encountered a problem NullPointerException Error

20. Remove Trailing zeros BigDecimal Java

    more...






Popular tags
android
x 175
eclipse
x 29
notepadplusplus
x 20
macosx
x 14
sharepoint
x 14
html
x 14
mac
x 13
androidstudio
x 11




1000+ C Programs     PHP Tutorial     JSON Tutorial     Swift Tutorial     India Pinocdes     About Us     Privacy Policy


Code2care © 2012-17