You are on page 1of 6

Android Application Development Training Tutorial

For more info visit http://www.zybotech.in

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

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. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@

+id/LinearLayout01" android:layout_width="fill_parent"

android:layout_height="fill_parent"

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"><LinearLayout android:id="@+id/LinearLayout02"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:orientation="horizontal"><EditText android:layout_width="wrap_content"

android:layout_height="wrap_content" android:width="170dip" android:id="@

+id/emailaddress"></EditText><TextView android:layout_width="wrap_content"

android:layout_height="wrap_content" android:id="@+id/emailaddress"

21.

android:text="Email address

"></TextView> 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. android:id="@+id/emailtext"></EditText> android:layout_height="wrap_content" android:lines="5" android:width="300dip" android:text="Subject"></TextView> </LinearLayout> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/emailsubject" +id/emailsubject"></EditText><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="170dip" android:id="@ android:orientation="horizontal"><EditText android:layout_width="wrap_content" android:layout_width="wrap_content" android:layout_height="wrap_content" <LinearLayout android:id="@+id/LinearLayout03" </LinearLayout>

44. <Button android:layout_width="wrap_content" android:layout_height="wrap_content"


A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

45. 46. 47. 48. 49. android:width="150dip"></Button> </LinearLayout> android:id="@+id/emailsendbutton" android:text="Send!"

Ugly, but works... Next we create a new class, called ....uhhhm...Email, then modify like this: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. public class Email extends Activity { Button send; EditText address, subject, emailtext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.email); send=(Button) findViewById(R.id.emailsendbutton); address=(EditText) findViewById(R.id.emailaddress); subject=(EditText) findViewById(R.id.emailsubject); emailtext=(EditText) findViewById(R.id.emailtext);
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText;

20. 21. 22. 23. 24. 25. 26. 27. final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 28. 29. 30. 31. emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ address.getText().toString()}); 32. 33. subject.getText()); 34. 35. emailtext.getText()); 36. 37. mail...")); 38. 39. 40. 41. 42. }
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

send.setOnClickListener(new OnClickListener() {

@Override public void onClick(View v) { // TODO Auto-generated method stub

emailIntent.setType("plain/text");

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,

Email.this.startActivity(Intent.createChooser(emailIntent, "Send

} }); }

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

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

You might also like