In this Tutorial we will see an example of how to send mail with attachment from android application , To send email from our application we need to make use of one of the native android action called   Intent.ACTION_SEND .
 
 
Lets See An Example
Project Detail
Project Name EmailDemo
Package com.pavan.emaildemo
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout


file: activity_main.xml
<ScrollView 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" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        tools:context=".MainActivity" >

        <EditText
            android:id="@+id/et_address_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="32dp"
            android:ems="10"
            android:hint="Email Address"
            android:inputType="textEmailAddress" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/et_subject_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/et_address_id"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="32dp"
            android:ems="10"
            android:hint="Subject" />

        <EditText
            android:id="@+id/et_message_id"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_alignLeft="@+id/et_subject_id"
            android:layout_below="@+id/et_subject_id"
            android:layout_marginTop="40dp"
            android:ems="10"
            android:hint="Message"
            android:inputType="textMultiLine"
            android:text="" />

        <Button
            android:id="@+id/bt_send_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/et_message_id"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30dp"
            android:text="SEND" />

        <Button
            android:id="@+id/bt_attach_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/et_subject_id"
            android:layout_below="@+id/et_subject_id"
            android:text="Attachment" />

        <TextView
            android:id="@+id/tv_attach_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/bt_attach_id"
            android:layout_alignBottom="@+id/bt_attach_id"
            android:layout_toRightOf="@+id/bt_attach_id"
            android:text=""
            android:textSize="12dp" />
    </RelativeLayout>

</ScrollView>


2. Activity


Attachment : For attaching a image file from gallery we make use of one of the native android action called Intent.ACTION_GET_CONTENT and we pass this action to the intent object and then this intent object is passed as a parameter to startActivityForResult method of an activity , data returned (i.e file) is stored in a Uri variable which is used in later in building emailIntent.

Sending : For sending a mail we make use of one of the native android action called   Intent.ACTION_SEND

file: MainActivity.java
package com.pavan.emaildemo;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

 EditText et_address, et_subject, et_message;
 String address, subject, message, file_path;
 Button bt_send, bt_attach;
 TextView tv_attach;

 private static final int PICK_IMAGE = 100;
 Uri URI = null;
 int columnindex;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initializeViews();
  bt_send.setOnClickListener(this);
  bt_attach.setOnClickListener(this);

 }

 private void initializeViews() {
  et_address = (EditText) findViewById(R.id.et_address_id);
  et_subject = (EditText) findViewById(R.id.et_subject_id);
  et_message = (EditText) findViewById(R.id.et_message_id);
  bt_send = (Button) findViewById(R.id.bt_send_id);
  bt_attach = (Button) findViewById(R.id.bt_attach_id);
  tv_attach = (TextView) findViewById(R.id.tv_attach_id);

 }

 @Override
 public void onClick(View v) {
  switch (v.getId()) {

  case R.id.bt_attach_id:
   openGallery();
   break;

  case R.id.bt_send_id:
   address = et_address.getText().toString();
   subject = et_subject.getText().toString();
   message = et_message.getText().toString();

   String emailAddresses[] = { address };

   Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

   emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
     emailAddresses);
   emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
   emailIntent.setType("plain/text");
   emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
   if (URI != null)
    emailIntent.putExtra(Intent.EXTRA_STREAM, URI);

   startActivity(emailIntent);

   break;

  }

 }

 private void openGallery() {
  Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  startActivityForResult(Intent.createChooser(intent, "Select Picture"),
    PICK_IMAGE);
 }

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {

   Uri selectedImage = data.getData();
   String[] filePathColumn = { MediaStore.Images.Media.DATA };

   Cursor cursor = getContentResolver().query(selectedImage,
     filePathColumn, null, null, null);
   cursor.moveToFirst();
   columnindex = cursor.getColumnIndex(filePathColumn[0]);
   file_path = cursor.getString(columnindex);
   // Log.e("Attachment Path:", attachmentFile);
   tv_attach.setText(file_path);
   URI = Uri.parse("file://" + file_path);
   cursor.close();
  }
 }
}

3. Manifest.xml


you need to add following four permissions to your manifest file
 
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />


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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pavan.emaildemo.MainActivity"
            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>


Next
Newer Post
Previous
This is the last post.

0 comments:

Post a Comment

 
Top