Search Wikipedia

Search results

Jun 25, 2015

How to Send Email

We  do not need to develop our email client from scratch because they are already available like Gmail and K9mail. But we will need to send email from our android application, where we will have to write an Activity that need to launch an email client and sends an email using our android device. For this purpose, our Acivity will send an ACTION_SEND along with appropriate data load, to the Android Intent Resolver. The specified chooser gives the proper interface for the user to pick how to send our email data.


STEP 1: INTENT OBJECT- Action to send Email


We will use ACTION_SEND action to launch an email client intalled on our Android device.
Following is simple syntax to create an intent with ACTION_SEND action.
Intent emailIntent = new Intent(Intent.ACTION_SEND);

STEP 2: INTENT OBJECT- Data/Type to send Email


To send an email we need to specify mailto: as URI using setData() method and datatype will be tottext/plain using setType() method as follows:
emailItent.setData(Uri.parse("mailto:"));
emailIntent.settype.settype("text/plain");

STEP 3: INTENT OBJECT- Extra to send Email


Android has built-in support to add TO,SUBJECT,CC,TEXT etc. fields which can be attached to intent before sending the intent to target email client.
Here is an example showing how to assign extra data to your intent:
emailIntent. putExtra(Intent.EXTRA_EMAIL,new String[]{"recipent@example.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT,"subject of email");
emailIntent.putExtra(Intent.EXTRA_TEXT,"body of email");

CODE:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/from_text"
        android:layout_weight="1"
        android:hint="From"/>
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/to_Text"
        android:layout_weight="1"
        android:hint="To"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/subject_Text"
        android:layout_weight="1"
        android:hint="Subject"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/message_Text"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:hint="Message here"/>

    <Button
        android:id="@+id/compose_email"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/compose_email"/>

</LinearLayout>

MainActivity.java
package com.example.kais.sendemaildemo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {

    String[] mStrings;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button composeEmail = (Button) findViewById(R.id.compose_email);
        composeEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendEmail();
            }
        });
    }

    protected  void sendEmail(){
        EditText  sender = (EditText)findViewById(R.id.from_text);
        EditText receiver = (EditText) findViewById(R.id.to_Text);
        EditText subject = (EditText) findViewById(R.id.subject_Text);
        EditText message = (EditText) findViewById(R.id.message_Text);

        String senderemail = sender.getText().toString();
        String receiveremail = receiver.getText().toString();
        String subemail = subject.getText().toString();
        String messageemail = message.getText().toString();

        Log.i("Send email", "");
        String[] TO = {receiveremail};
        String[] CC = {senderemail};
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("text/plain");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
        emailIntent.putExtra(Intent.EXTRA_CC, CC);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subemail);
        emailIntent.putExtra(Intent.EXTRA_TEXT, messageemail);
        try {
            startActivity(Intent.createChooser(emailIntent, "Send mail..."));
            finish();
            Log.i("Finished sending email...", "");
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MainActivity.this,
                    "There is no email client installed.", Toast.LENGTH_SHORT).show();
        }

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}




No comments:

Post a Comment

Did this post help you? Do you have any questions? Drop your thoughts here...

}