RSS

Search Engine

Saturday, December 25, 2010

Send email with attached file in Android

Hello Friends,

Today i have discussed about how to send email with attached file in android.

In android u have to give internet permission for android manifest file.

And this application work only from device not in android emulator... so this think is very important... And Please Refer this Android Application Website.

And source code given below.

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01"

android:layout_width="fill_parent" android:layout_height="fill_parent"

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical">

<LinearLayout android:id="@+id/LinearLayout02"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:orientation="horizontal">

<EditText android:layout_width="wrap_content"

android:layout_height="wrap_content" android:width="170dip"

android:id="@+id/emailaddress"></EditText>

<TextView android:layout_width="wrap_content"

android:layout_height="wrap_content" android:id="@+id/emailaddress"

android:text="Email address"></TextView>

</LinearLayout>

<LinearLayout android:id="@+id/LinearLayout03"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:orientation="horizontal">

<EditText android:layout_width="wrap_content"

android:layout_height="wrap_content" android:width="170dip"

android:id="@+id/emailsubject"></EditText>

<TextView android:layout_width="wrap_content"

android:layout_height="wrap_content" android:id="@+id/emailsubject"

android:text="Subject"></TextView>

</LinearLayout>

<EditText android:layout_width="wrap_content"

android:layout_height="wrap_content" android:lines="5" android:width="300dip"

android:id="@+id/emailtext"></EditText>



<Button android:layout_width="wrap_content"

android:layout_height="wrap_content" android:id="@+id/emailsendbutton"

android:text="Send!" android:width="150dip"></Button>



</LinearLayout>



And java file given also below.

Email.java

package com.fb

import java.io.file;

import android.app.Activity;

import android.net.Uri;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

public class Email Activity {

Button send;

EditText address, subject, emailtext;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

send = (Button) findViewById(R.id.emailsendbutton);

address = (EditText) findViewById(R.id.emailaddress);

subject = (EditText) findViewById(R.id.emailsubject);

emailtext = (EditText) findViewById(R.id.emailtext);

send.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

final Intent emailIntent = new Intent(

android.content.Intent.ACTION_SEND);

emailIntent.setType("image/png");

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,

new String[] { address.getText().toString() });

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,

subject.getText());

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,

emailtext.getText());

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri

.parse("android.resource://"

+ getPackageName() + "/" + R.drawable.icon));

FB.this.startActivity(Intent.createChooser(emailIntent,

"Send mail..."));

}

});

}

}

So this are the simple code for send email with attcahed file in android

I think u have enjoyed this code.

18 comments:

wHiTeHaT said...

i tested it on my htc legend but had to make some changes to let it operate:

code:

package com.fb;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Email extends Activity {

Button send;

EditText address, subject, emailtext;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

send = (Button) findViewById(R.id.emailsendbutton);

address = (EditText) findViewById(R.id.emailaddress);

subject = (EditText) findViewById(R.id.emailsubject);

emailtext = (EditText) findViewById(R.id.emailtext);

send.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

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

emailIntent.setType("image/png");

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { address.getText().toString() });

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText());

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+ getPackageName() + "/" + R.drawable.icon));

Email.this.startActivity(Intent.createChooser(emailIntent,"Send mail..."));

}

});

}

}

wHiTeHaT said...

BTW it also misses the subject evertime i try to send email.
Whenever it leaves the template it goto chosen cliƫnt (in my case i choose for Gmail).

I also expexted there would be a file chooser instead of a default icon.

But hey...you do good work.

I also recommend you use a better code igniter on your website , becouse when you copy/paste it... you must manualy edit each line.

Anonymous said...

Can you pls POST the code to get Primary account EMail address?

sheetal said...

Hi....

but i want to add image in compose body nt as an attachment......is there any way to add image from sd card into email body.....

nehal said...

I am getting this err:
"No Applications can perform this action"
please suggest! thanx

Mayuri said...

can you please tell me how to check the email delivery report

Lenn Dolling said...

whoo hoo! Nice. Please help! Vote!

please vote!! I need everyone's help! http://fluapp.challenge.gov/submissions/3027-cdcs-flu-weekly-sneeze-in-your-sleeve

or on http://8cupsaday.com click the vote!

rakesh said...

I need to append the image in mail body, not as an attachment

plz help me

Anonymous said...

hi, can you send post it in the zip file?

Sanat said...

This is not working for gmail app in android. How this will work for same?

Sanat said...

How we will send images from url in attachment.

don said...

but on opening this file image is not opening as image

Anonymous said...

Hi, this is not working it show error in the line Email.this.startActivity(Intent.createChooser(emailIntent,"Send mail..."));
please suggest me any idea

Anonymous said...

no work curecttly because i need another informaion about permission

Rajkumari Parihar said...

hi ,
How can we send image as well as cvs or text file with email attachement.
Please help me i want to use in my application

Anonymous said...

STEP BY STEP ANDROID TUTORIALS

VISIT:-www.androidituts.blogspot.com

Tony Abraham said...

add this line also.nw subject also adding
emailIntent.setType("text/plain");

mrigang dwivedi said...

Hi ,

I' m new to Android app development. I am trying to create a mobile application that has three entry fields. i.e: Name , Contact Number , Email Address( Receiver's). I want to email the first and second entry of the input data captured to the email address (the third entry ) on click of Send button.

Thanks
Mrigang

Post a Comment