AdSense

AdSense3

Monday, 26 October 2015

How to send email in android using intent

We can easily send email in android via intent. You need to write few lines of code only as given below

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

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
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/editText1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentRight="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginRight="22dp"  
  14.         android:layout_marginTop="16dp"  
  15.         android:ems="10" />  
  16.   
  17.     <EditText  
  18.         android:id="@+id/editText2"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignLeft="@+id/editText1"  
  22.         android:layout_below="@+id/editText1"  
  23.         android:layout_marginTop="18dp"  
  24.         android:ems="10" >  
  25.   
  26.     </EditText>  
  27.   
  28.     <EditText  
  29.         android:id="@+id/editText3"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:layout_alignLeft="@+id/editText2"  
  33.         android:layout_below="@+id/editText2"  
  34.         android:layout_marginTop="28dp"  
  35.         android:ems="10"  
  36.         android:inputType="textMultiLine" />  
  37.   
  38.     <TextView  
  39.         android:id="@+id/textView1"  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"  
  42.         android:layout_alignBaseline="@+id/editText1"  
  43.         android:layout_alignBottom="@+id/editText1"  
  44.         android:layout_alignParentLeft="true"  
  45.         android:text="To:" />  
  46.   
  47.     <TextView  
  48.         android:id="@+id/textView2"  
  49.         android:layout_width="wrap_content"  
  50.         android:layout_height="wrap_content"  
  51.         android:layout_alignBaseline="@+id/editText2"  
  52.         android:layout_alignBottom="@+id/editText2"  
  53.         android:layout_alignParentLeft="true"  
  54.         android:text="Subject:" />  
  55.   
  56.     <TextView  
  57.         android:id="@+id/textView3"  
  58.         android:layout_width="wrap_content"  
  59.         android:layout_height="wrap_content"  
  60.         android:layout_alignBaseline="@+id/editText3"  
  61.         android:layout_alignBottom="@+id/editText3"  
  62.         android:layout_alignParentLeft="true"  
  63.         android:text="Message:" />  
  64.   
  65.     <Button  
  66.         android:id="@+id/button1"  
  67.         android:layout_width="wrap_content"  
  68.         android:layout_height="wrap_content"  
  69.         android:layout_alignLeft="@+id/editText3"  
  70.         android:layout_below="@+id/editText3"  
  71.         android:layout_marginLeft="76dp"  
  72.         android:layout_marginTop="20dp"  
  73.         android:text="Send" />  
  74.   
  75. </RelativeLayout>  

Activity class

Let's write the code to send email via intent.
File: MainActivity.java
  1. package com.example.sendemail;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     EditText editTextTo,editTextSubject,editTextMessage;  
  14.     Button send;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.           
  20.         editTextTo=(EditText)findViewById(R.id.editText1);  
  21.         editTextSubject=(EditText)findViewById(R.id.editText2);  
  22.         editTextMessage=(EditText)findViewById(R.id.editText3);  
  23.           
  24.         send=(Button)findViewById(R.id.button1);  
  25.           
  26.         send.setOnClickListener(new OnClickListener(){  
  27.   
  28.             @Override  
  29.             public void onClick(View arg0) {  
  30.                  String to=editTextTo.getText().toString();  
  31.                  String subject=editTextSubject.getText().toString();  
  32.                  String message=editTextMessage.getText().toString();  
  33.                    
  34.                   
  35.                  Intent email = new Intent(Intent.ACTION_SEND);  
  36.                   email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});  
  37.                   email.putExtra(Intent.EXTRA_SUBJECT, subject);  
  38.                   email.putExtra(Intent.EXTRA_TEXT, message);  
  39.        
  40.                   //need this to prompts email client only  
  41.                   email.setType("message/rfc822");  
  42.        
  43.                   startActivity(Intent.createChooser(email, "Choose an Email client :"));  
  44.        
  45.             }  
  46.               
  47.         });  
  48.     }  
  49.   
  50.     @Override  
  51.     public boolean onCreateOptionsMenu(Menu menu) {  
  52.         // Inflate the menu; this adds items to the action bar if it is present.  
  53.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  54.         return true;  
  55.     }  
  56.   
  57. }  

No comments:

Post a Comment