Open top menu


In this blog explain how to send email in android. the email send via Intent ,the intent carry data one component to another means your message ,subject and email id  etc .check uploaded screen shot.






Used Intent.
 Intent email = new Intent(Intent.ACTION_SEND);
                  email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
                  email.putExtra(Intent.EXTRA_SUBJECT, subject);
                  email.putExtra(Intent.EXTRA_TEXT, message);


activity_main.xml.

Create the class inside the res/layout folder and drop down three edittext and send button.
...................................................................................................................................................................

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

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_margin="5dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="To"
        android:inputType="textMultiLine" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_margin="5dp"
        android:layout_marginTop="18dp"
        android:ems="10"
        android:hint="Subject"
        android:inputType="textMultiLine" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_margin="5dp"
        android:layout_marginTop="18dp"
        android:ems="10"
        android:hint="Message"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/sendBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText3"
        android:layout_marginTop="20dp"
        android:text="Send" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="31dp"
        android:src="@drawable/logo" />

</RelativeLayout>

...................................................................................................................................................................

MainActivity.java
...................................................................................................................................................................


package com.example.androidtestcode;

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 MainActivity extends Activity {

EditText editTo,editSubject,editMessage;
    Button send;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTo=(EditText)findViewById(R.id.editText1);
editSubject=(EditText)findViewById(R.id.editText2);
editMessage=(EditText)findViewById(R.id.editText3);
       
        send=(Button)findViewById(R.id.sendBtn);
       
        send.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                 String to=editTo.getText().toString();
                 String subject=editSubject.getText().toString();
                 String message=editMessage.getText().toString();
                 
               
                  Intent email = new Intent(Intent.ACTION_SEND);
                  email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
                  email.putExtra(Intent.EXTRA_SUBJECT, subject);
                  email.putExtra(Intent.EXTRA_TEXT, message);
     
               
                  email.setType("message/rfc822");      
                  startActivity(Intent.createChooser(email, "Choose an Email client side :"));
     
            }


           
        });

       
   
}


}


...................................................................................................................................................................




Tagged

0 comments