Android tutorial - Android Intent | Android Send Email Intent - android app development - android studio - android development tutorial



Android send email intent

Learn android - android tutorial - Android send email intent - android examples - android programs

  • Email is messages distributed by electronic means from one system user to one or more recipients via a network.
  • To send an email from your application, you don’t have to implement an email client ,
  •  sharing messages from various options of clients in android
  • the beginning, but you can use an existing one like the default Email app provided from Android, Gmail, Outlook, K-9 Mail etc.
  • Sending mail is one key feature in android and an easy one as well.
 compose messages, subject, client address from email in android
  • We can easily send email in android via intent. You need to write few lines of code only as given below
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 :"));  
click below button to copy the code from android tutorial team

Example of phone call in android

activity_main.xml

  • Drag the 2 EditTexts, 1 MultiLine EditText, 3 TextViews and 1 Button from the pallete, now the activity_main.xml file will like this:
  • File: activity_main.xml
<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MainActivity" >  
  
    <EditText  
        android:id="@+id/editText1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentRight="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginRight="22dp"  
        android:layout_marginTop="16dp"  
        android:ems="10" />  
  
    <EditText  
        android:id="@+id/editText2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/editText1"  
        android:layout_below="@+id/editText1"  
        android:layout_marginTop="18dp"  
        android:ems="10" >  
  
        <requestFocus />  
    </EditText>  
  
    <EditText  
        android:id="@+id/editText3"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/editText2"  
        android:layout_below="@+id/editText2"  
        android:layout_marginTop="28dp"  
        android:ems="10"  
        android:inputType="textMultiLine" />  
  
    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/editText1"  
        android:layout_alignBottom="@+id/editText1"  
        android:layout_alignParentLeft="true"  
        android:text="To:" />  
  
    <TextView  
        android:id="@+id/textView2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/editText2"  
        android:layout_alignBottom="@+id/editText2"  
        android:layout_alignParentLeft="true"  
        android:text="Subject:" />  
  
    <TextView  
        android:id="@+id/textView3"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/editText3"  
        android:layout_alignBottom="@+id/editText3"  
        android:layout_alignParentLeft="true"  
        android:text="Message:" />  
  
    <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/editText3"  
        android:layout_below="@+id/editText3"  
        android:layout_marginLeft="76dp"  
        android:layout_marginTop="20dp"  
        android:text="Send" />  
  
</RelativeLayout>  
click below button to copy the code from android tutorial team

Activity class

  • Let's write the code to send email via intent.
  • File: MainActivity.java
package com.example.sendemail;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.content.Intent;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
  
public class MainActivity extends Activity {  
    EditText editTextTo,editTextSubject,editTextMessage;  
    Button send;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        editTextTo=(EditText)findViewById(R.id.editText1);  
        editTextSubject=(EditText)findViewById(R.id.editText2);  
        editTextMessage=(EditText)findViewById(R.id.editText3);  
          
        send=(Button)findViewById(R.id.button1);  
          
        send.setOnClickListener(new OnClickListener(){  
  
            @Override  
            public void onClick(View arg0) {  
                 String to=editTextTo.getText().toString();  
                 String subject=editTextSubject.getText().toString();  
                 String message=editTextMessage.getText().toString();  
                   
                  
                 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 :"));  
       
            }  
              
        });  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.activity_main, menu);  
        return true;  
    }  
  
}  
click below button to copy the code from android tutorial team

Related Searches to Android Intent | Android Send Email Intent