Wednesday 30 May 2012

Sending Email in Android

In Android, you can use Intent.ACTION_SEND to call an existing email client to send an Email.


Follow the below steps:


Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]
{"youremail@gmail.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
email.setType("plain/text");
startActivity(Intent.createChooser(email,"Send mail..."));


Here ACTION_SEND used for Deliver some data to someone else.Who the data
is being delivered to is not specified; it is up to the receiver of this action to ask the
user where the data should be sent.
When launching a SEND intent, you should usually wrap it in a chooser (through
createChooser(Intent, CharSequence)), which will give the proper interface for the
user to pick how to send your data and allow you to specify a prompt  indicating
what they are doing.
Input: setType() is the MIME type of the data being sent. putExtra can have either
a EXTRA_TEXT or EXTRA_STREAM field, containing the data to be sent. If using
EXTRA_TEXT, the MIME type should be "text/plain".


Here is my main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
LinearLayout=""
android:orientation="vertical" >


    <LinearLayout
        android:id="@+id/LinearLayout02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <EditText
            android:id="@+id/emailaddress"   
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="170dip" >
        </EditText>


        <TextView
            android:id="@+id/emailaddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Email address" >
        </TextView>
    </LinearLayout>


    <LinearLayout
        android:id="@+id/LinearLayout03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <EditText
            android:id="@+id/emailsubject"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="170dip" >
        </EditText>


        <TextView
            android:id="@+id/emailsubject"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Subject" >
        </TextView>
    </LinearLayout>


    <EditText
        android:id="@+id/emailtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lines="5"
        android:width="300dip" >
    </EditText>


    <Button
        android:id="@+id/emailsendbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        Send=""
        android:width="150dip" >
    </Button>


</LinearLayout>


My class file is:
public class Email extends Activity {
    Button send;
    EditText address, subject, emailtext;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    send=(Button) findViewById(R.id.emailsendbutton);
    address=(EditText) findViewById(R.id.emailaddress);
    subject=(EditText) findViewById(R.id.emailsubject);
    emailtext=(EditText) findViewById(R.id.emailtext);
  
    send.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            // TODO Auto-generated method stub
          final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
         emailIntent.setType("plain/text");
         emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {address.getText().toString()});                            
         emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());
         emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText());
         Email.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));


                    }
            });
}
}


Note
Android does not provide API to send Email directly, you have to call the existing Email client to send Email.
For this we need to configure Email client in our device/Emulator.

No comments:

Post a Comment