How to send email from your application


SDK Version: 
M3

Today we'll create an easy email sender application.
First of all we need to create a layout to set the address, the subject and email body box.

  1. <?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@
  2.  
  3. +id/LinearLayout01" android:layout_width="fill_parent"
  4.  
  5. android:layout_height="fill_parent"
  6.  
  7. xmlns:android="http://schemas.android.com/apk/res/android"
  8.  
  9. android:orientation="vertical"><LinearLayout android:id="@+id/LinearLayout02";
  10.  
  11. android:layout_width="wrap_content" android:layout_height="wrap_content"
  12.  
  13. android:orientation="horizontal"><EditText android:layout_width="wrap_content"
  14.  
  15. android:layout_height="wrap_content" android:width="170dip" android:id="@
  16.  
  17. +id/emailaddress"></EditText><TextView android:layout_width="wrap_content"
  18.  
  19. android:layout_height="wrap_content" android:id="@+id/emailaddress"
  20.  
  21. android:text="Email address"></TextView>
  22. </LinearLayout>
  23.  
  24.  
  25. <LinearLayout android:id="@+id/LinearLayout03";
  26.  
  27. android:layout_width="wrap_content" android:layout_height="wrap_content"
  28.  
  29. android:orientation="horizontal"><EditText android:layout_width="wrap_content"
  30.  
  31. android:layout_height="wrap_content" android:width="170dip" android:id="@
  32.  
  33. +id/emailsubject"></EditText><TextView android:layout_width="wrap_content"
  34.  
  35. android:layout_height="wrap_content" android:id="@+id/emailsubject"
  36.  
  37. android:text="Subject"></TextView>
  38. </LinearLayout>
  39. <EditText android:layout_width="wrap_content"
  40.  
  41. android:layout_height="wrap_content" android:lines="5" android:width="300dip"
  42.  
  43. android:id="@+id/emailtext"></EditText>
  44. <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
  45.  
  46. android:id="@+id/emailsendbutton&quot; android:text="Send!"
  47.  
  48. android:width="150dip"></Button>
  49. </LinearLayout>

Ugly, but works...
Next we create a new class, called ....uhhhm...Email, then modify like this:

  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8.  
  9. public class Email extends Activity {
  10.         Button send;
  11.         EditText address, subject, emailtext;
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.email);
  16.         send=(Button) findViewById(R.id.emailsendbutton);
  17.         address=(EditText) findViewById(R.id.emailaddress);
  18.         subject=(EditText) findViewById(R.id.emailsubject);
  19.         emailtext=(EditText) findViewById(R.id.emailtext);
  20.        
  21.         send.setOnClickListener(new OnClickListener() {
  22.                        
  23.                         @Override
  24.                         public void onClick(View v) {
  25.                                 // TODO Auto-generated method stub
  26.                                  
  27.                                       final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
  28.                                
  29.                                       emailIntent.setType("plain/text");
  30.                                  
  31.                                       emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ address.getText().toString()});
  32.                                
  33.                                       emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());
  34.                                
  35.                                       emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText());
  36.                        
  37.                                     Email.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
  38.  
  39.                         }
  40.                 });
  41.     }
  42. }

It will use the button's onclicklistener method to send the email. It does not work on emulator, but works on real devices.