Android Code to Send Email

There are numerous mobile android applications in which the email option has been integrated and nowadays in professional official mobile applications in android has the email option featured within the app. In this post you can learn on writing android codes on how to send an email. It will ask for gmail client.

The below android source code is an example to send Email from an android application. Include this in the activity file of your project and the work is done and thats it.

Intent mAndroidEmailIntent = new Intent (android.content.Intent.ACTION_SEND); String aEmailList[] = { "android@gmail.com","android@yahoo.com" }; mAndroidEmailIntent.putExtra (android.content.Intent.EXTRA_EMAIL, aEmailList); mAndroidEmailIntent.putExtra (android.content.Intent.EXTRA_SUBJECT, "Mail Subject"); mAndroidEmailIntent.setType ("plain/text"); mAndroidEmailIntent.putExtra (android.content.Intent.EXTRA_TEXT, "Email From My Android App"); startActivity (mAndroidEmailIntent); 

Now let us see the steps for sending email in an android app in brief below.

1.  Create the Activity_Main.xml file
activity_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>


2. Now create the ActivityMain.java file
ActivityMain.java

package com.udhaya.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 source code is the key for sending the 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);
            }
        });
    }
}


3. Now work on the Android Manifest file. Add the below Uses Permission for internet options.
   <uses-permission android:name="android.permission.INTERNET"/>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.udhaya.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.udhaya.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:

Post a Comment

My Profile