Apr 6, 2011

How to send email from your application

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.
      
Note :
===
1-> when we are developing Any internet based application we should add 
 below tag in manifest file :
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

2-> this application won't work in Emulator

3->  our device should be contained net internet connection 

                                                                                                                                                               

public static final String ACTION_SEND


Activity Action: 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: getType() is the MIME type of the data being sent. get*Extra 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"; otherwise it should be the MIME type of the data in EXTRA_STREAM. Use */* if the MIME type is unknown (this will only allow senders that can handle generic data streams).

Optional standard extras, which may be interpreted by some recipients as appropriate, are: EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECT.

Output: nothing.
Constant Value: "android.intent.action.SEND"


main.xml file :
------------------------
<?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"
            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:layout_width="wrap_content"
            android:layout_height="wrap_content" android:width="170dip"
            android:id="@+id/emailsubject"></EditText>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/emailsubject"
            android:text="Subject"></TextView>
</LinearLayout>
<EditText android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:lines="5" android:width="300dip"
        android:id="@+id/emailtext"></EditText>
    <Button
    android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/emailsendbutton"  android:text="Send" android:width="150dip">
        </Button>
</LinearLayout>


Email.java :
-----------------
package sra.emia;

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;

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) {
                // getting values and stored in particula intent
                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());
                Email.this.startActivity(Intent.createChooser(emailIntent,
                        "Send mail..."));
            }
        });
    }
}
When we are running our app in Emulator we will get this type of message ( means not working )



  
When we use Device we can sent Sucessfully with specifed id by our device holder 

    







 

No comments:

Post a Comment