Email Activity

How to send Email : 

In order to send email you need to write following piece of code. We created an Intent Object email that uses Intent.ACTION_SEND to call an existing email client to send an Email.

Note :

Run & test on real device only.
If you run this on emulator, you will hit error message : “No application can perform this action“. This code only work on real device.

MainActivity.java : 

public class MainActivity extends Activity {

       EditText et_to,et_subject,et_msg;
       Button send;
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              send = (Button) findViewById(R.id.button1);
              et_to = (EditText) findViewById(R.id.editTextTo);
              et_subject = (EditText) findViewById(R.id.editTextSubject);
              et_msg = (EditText) findViewById(R.id.editTextMessage);

              send.setOnClickListener(new OnClickListener() {
                    
                     @Override
                     public void onClick(View arg0) {
                                 
                           String to = et_to.getText().toString();
                           String sub = et_subject.getText().toString();
                           String msg = et_msg.getText().toString();
                          
                           Intent email = new Intent(Intent.ACTION_SEND);
                           email.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
                           email.putExtra(Intent.EXTRA_SUBJECT, sub);
                           email.putExtra(Intent.EXTRA_TEXT, msg);
                           email.setType("message/rfc822");
                           startActivity(Intent.createChooser(email, "sending email.."));
                     }
              });
             
             
       }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

}


activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editTextTo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:inputType="textEmailAddress" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignParentTop="true"
        android:text="To :" />

    <EditText
        android:id="@+id/editTextSubject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="26dp"
        android:ems="10" />

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextMessage"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="37dp"
        android:layout_toRightOf="@+id/textView3"
        android:text="Send" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextSubject"
        android:layout_below="@+id/editTextSubject"
        android:layout_marginTop="28dp"
        android:text="Message :" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextTo"
        android:layout_below="@+id/editTextTo"
        android:layout_marginTop="22dp"
        android:text="Subject :" />


</RelativeLayout>






No comments:

Post a Comment