Monday, January 13, 2014

How to Send Email in Android Programmatically

Share it Please
Today we will learn how to Send Email in android Programmatically. To demonstrate this , I have made a demo project named SendEmail. In this project I have three Textview one for Email Address, one for Subject and last one for message. I am also having one send button,on the tap of it a chooser will popup asking for  Gmail client.

Step 1:-  create main.xml

<LinearLayout 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:orientation="vertical" >


    <EditText
        android:id="@+id/editText_emailAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Email"
        android:inputType="textEmailAddress" >
    </EditText>


    <EditText
        android:id="@+id/editText_subject"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Subject" >
    </EditText>


    <MultiAutoCompleteTextView
        android:id="@+id/multiAutoCompleteTextView_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Message" >
    </MultiAutoCompleteTextView>


    <Button
        android:id="@+id/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Send " />


</LinearLayout>

Step 2:- ActivityMain.java

package com.arpit.sendemail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class ActivityMain extends Activity {

    TextView emailadd, sub, message;
    Button btnsend;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        emailadd = (TextView) findViewById(R.id.editText_emailAdd);
        sub = (TextView) findViewById(R.id.editText_subject);
        message = (TextView) findViewById(R.id.multiAutoCompleteTextView_message);
        btnsend = (Button) findViewById(R.id.button_send);


        btnsend.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                String emailAddress = emailadd.getText().toString();
                String emailSubject = sub.getText().toString();
                String emailMessage = message.getText().toString();


                // below is the code for sending an email
                Intent emailIntent = new Intent(
                        android.content.Intent.ACTION_SEND);
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                        emailAddress);
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                        emailSubject);
                emailIntent.setType("plain/text");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                        emailMessage);
                startActivity(emailIntent);


            }

        });

    }

}

Step 3:- Manifest file

Need to add internet permission in the manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android"
    package="com.arpit.sendemail"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

   <uses-permission android:name="android.permission.INTERNET"/>


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.arpit.sendemail.ActivityMain"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>







No comments:

About Us

I, Bimal K. Chawla, Working in Android technology as Associate Software Engineer in Mohali, Punjab, India. I likes to play and watch cricket, to Walk...Read More

Blogroll

Advertisment

About