Wednesday, March 27, 2013

Sending E-mail through your Application using Mail Clients


It may possible that we need to send E-mail through our Application, The Code is given below for sending E-mail. It uses the mail client installed in Android phone, Oftenly it doesn't work on the emulator because emulator doesn't have a pre-installed mail-client, we need to install E-mail Client first on emulators then this will work on emulator, you can directly run this your phone.

On Emulator Since no client installed, it'll work fine on phone.


<!-- --------------------------------------- XML file------------------------------------------------- -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
        android:id="@+id/fp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="forget Password?"
        android:layout_gravity="center" />

</LinearLayout>
<!-- ------------------------- End of XML file------------------------------------------------- -->



/*======================== Java File ==================================*/
public class forget extends Activity {
public void onCreate(Bundle savedInstanceActivity ){
super.onCreate(savedInstanceActivity);
setContentView(R.layout.authenticate);

TextView fp = (TextView)findViewById(R.id.fp);
fp.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
try{
Uri uri = Uri.parse("mailto:ashutiwari4@gmail.com");
Intent emailIntent = new Intent(Intent.ACTION_SENDTO,uri);
emailIntent.setType("message/rfc822");
final String aEmailList[]  = {"ashutiwari4@gmail.com"};
emailIntent.putExtra(Intent.EXTRA_EMAIL, aEmailList);
emailIntent.putExtra(Intent.EXTRA_SUBJECT,"Sample Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "this is extra");
startActivity(Intent.createChooser(emailIntent, "Select Email Application"));
}
catch(Exception ex){
Toast.makeText(forget.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
Log.e("SendEmail","Can't send email",ex);
}
}
});
}
}

/*===================== End of Java File ============================*/

No comments:

Post a Comment