This is a simple demo for send email in Android with attachment. For attachment I am using Intent.ACTION_GET_CONTENT.
Don't forget to add permissions in your manifest.xml-
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1)MainActivity.java
2)activity_main.xml
Don't forget to add permissions in your manifest.xml-
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1)MainActivity.java
package
com.manish.sendemaildemo;
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.util.Log;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.Toast;
/**
*
* @author manish
*
*/
public class MainActivity extends Activity implements OnClickListener {
EditText
editTextEmail, editTextSubject, editTextMessage;
Button
btnSend, btnAttachment;
String
email, subject, message, attachmentFile;
Uri
URI = null;
private static final int PICK_FROM_GALLERY = 101;
int columnIndex;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextEmail = (EditText)
findViewById(R.id.editTextTo);
editTextSubject = (EditText)
findViewById(R.id.editTextSubject);
editTextMessage = (EditText)
findViewById(R.id.editTextMessage);
btnAttachment = (Button)
findViewById(R.id.buttonAttachment);
btnSend = (Button)
findViewById(R.id.buttonSend);
btnSend.setOnClickListener(this);
btnAttachment.setOnClickListener(this);
}
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (requestCode == PICK_FROM_GALLERY &&
resultCode == RESULT_OK) {
/**
* Get Path
*/
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]);
attachmentFile = cursor.getString(columnIndex);
Log.e("Attachment
Path:", attachmentFile);
URI = Uri.parse("file://" + attachmentFile);
cursor.close();
}
}
@Override
public void onClick(View v) {
if (v == btnAttachment) {
openGallery();
}
if (v == btnSend) {
try {
email = editTextEmail.getText().toString();
subject = editTextSubject.getText().toString();
message = editTextMessage.getText().toString();
final Intent emailIntent =
new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { email });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
subject);
if (URI != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
}
emailIntent
.putExtra(android.content.Intent.EXTRA_TEXT, message);
this.startActivity(Intent.createChooser(emailIntent,
"Sending
email..."));
}
catch (Throwable t) {
Toast.makeText(this,
"Request
failed try again: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}
}
public void openGallery() {
Intent
intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(intent,
"Complete
action using"),
PICK_FROM_GALLERY);
}
}
<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"
android:padding="5dp"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:padding="5dp" >
<EditText
android:id="@+id/editTextTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:hint="Email Address!"
android:inputType="textEmailAddress"
android:singleLine="true" />
<EditText
android:id="@+id/editTextSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextTo"
android:layout_margin="5dp"
android:hint="Subject"
android:singleLine="true" />
<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/editTextSubject"
android:layout_margin="5dp"
android:gravity="top|left"
android:hint="type message here!"
android:inputType="textMultiLine" />
<Button
android:id="@+id/buttonSend"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_below="@id/editTextMessage"
android:layout_margin="5dp"
android:text="Send" />
<Button
android:id="@+id/buttonAttachment"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="attachment" />
</RelativeLayout>
</ScrollView>
3)AndroidManifest.xml
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manish.sendemaildemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"
/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"
/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.manish.sendemaildemo.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>
Thanks!
HI manish have checked with working device the code was not performing any operations on it.
ReplyDeleteIt should work dear! I have tested it on many devices. Something wrong I think. Please check permission, package name.
Deleteimage is not uploading
DeleteHi manish in this application give me error " No application can perform this action". how to solve it.
ReplyDeletethanks
You are using emulator for testing? If you are using device please use some application for send email first like yahoo, gmail etc. You can sync a gmail account with your mobile or emulator...
DeleteThanks!
how to set up internet on the emulator?
DeleteHi manish,,, i got an error in mainactivity.java... "activity_main cannot be resolved or is not a field". how to solf it?
ReplyDeletethanks
I think in your project R.java file is not created. please clean and re-build your project.
DeleteHello manish, this s working .......this application want to test in device ......great work manish
ReplyDeletehiii manishb great work...Can I send My sqlite data using it should display in table format in email
ReplyDeleteYou can't send it directly from sqite. You need to generate report first in pdf or any other format after that you can send as attachment.
DeleteThank you manish..If I dont want use Third Party Api then there is any solution without using any Api can we save data in Html..if you have source code of how generate report in Android then please suggest
DeleteSorry dear no idea. Well if you want do that you can use web-service to send your data on server and from there do what you want.
Deletesry, i have a question :( it not run with my adt, can you send project to me dinhthehoa.aks@gmail.com i ready need it for big my homework. Please!!!!! Thank you
ReplyDeleteOkay I am sending zip code please try to import into your workspace.
DeleteHey email is going successfully but without an attachment.
ReplyDeletecan you tell why is this happening?
please check your log-cat is there path of image or not? may be your path==null.
DeleteThis absolutely works. Thanks alot
ReplyDeleteYour welcome!
Deletegreat job dude
ReplyDeleteThanks Dear!
Deletehi manish
ReplyDeletei am new to this android coding
i was trying for something else and i got this code i tried it, it is working fine but when i click on attachment it is directing me to gallery and if i click on any image it is not picking up the file plz help me
thank you very much.
how to send image file as attachment from my project assets folder its very urgent can someone please help me
ReplyDeleteSir, i want the zip file.
ReplyDeletei am new in android and unable to run this program perfectly . so, I need your help
my email id : rshrwan@gmail.com
Please copy paste from above it is very simple program. It should work properly. Actually I don't have this code on GIT server.
DeleteHii Manish,
ReplyDeleteHow to add the images into emulator gallery?can you please let me know th esolution?
Use DDMS in eclipse. Using DDMS you can put an image into your sdcard.
DeleteI always that I select a archive, Always get a error nullpointer : NOT WORK correctly al least > 4.0
ReplyDelete04-24 02:24:58.509: E/AndroidRuntime(9779): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=101, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:18 flg=0x1 }} to activity {com.manish.sendemaildemo/com.manish.sendemaildemo.MainActivity}: java.lang.NullPointerException: println needs a message
As it shows nullpointer exception its mean your data is null when you trying to get onActivityResult.
DeleteSo use try catch here. And use any other method to get the url of image.
public void openGallery() {
DeleteIntent intent=new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, GALARY_REGUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
URI=data.getData();
}
Try this)
Great job :)
DeleteHii manish i want to add my pdf files into attachment.....It can be possible to add pls help me ...
ReplyDeletethnx for before posts..
Yes sure you can send any type of file. Just pass your URI of your PDF file.
DeleteAnd for user Interface you need to implement chooser lib like yahoo mail app using.
Hi Manish my name is Arslanali. It is necessary to send a xml file by email. I do not understand how to send a file. You can not find an example of sending a file?
DeleteThanks Manish
This comment has been removed by the author.
ReplyDeleteHii manish i want to add my pdf files into attachment.....It can be possible to adding attachment pls help me ...
ReplyDeletethnx for before posts..
Yes sure you can send any type of file. Just pass your URI of image.
DeleteAnd for user Interface you need to implement chooser lib like yahoo mail app using.
Hi manish, actually i'm in trouble with my small project, wil you help me with pick / send image to a mail id,(where i have camera interface from where i capture image and captured image should be sent to a particular (fixed) mail id ).. i'm new to android so facing lot of problems hope you eil help..
ReplyDeleteWhat if we want to pick image and send by one button only? This is you attach first and send. It should send immediately after attached. Any tutorial? Thank you.
ReplyDeletehi manish, please help i just want the form to send the mail to a pre defined email
ReplyDeleteSo just pass your email id here-
DeleteemailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { xyz@gmail.com});
Hello Sir, Thnxx a lot.. after making some change, this project i.e. working superb... thank you so so much for such type article.. I really appreciate your effort and salute to you.. Please teach us hoe to capture image and send it to send to ListView as well as choose pic from gallery... thnxx a lot fro such type article again.
ReplyDeleteTo display image in listview first you need to store images into local storage or sqlite or any collection. Kindly follow below below url, it will help you using sqlite database to display image in listview.
Deletehttp://www.androidhub4you.com/2013/04/sqlite-example-in-android-add-image.html
Hello, What if i don't want the user to send the email to anyone but one email address. How do i go about doing that?
ReplyDeleteIn case of chooser create new email page will be open and user can send email to anyone.For your requirement you need to use web-service and send email from server to particulate email id.
DeleteOh okay. Thank you very much. That is very helpful.
DeleteHow to Attached .doc file.
ReplyDeleteYou need to user chooser, follow below link to create chooser-
Deletehttp://www.androidhub4you.com/2014/12/android-zip-unzip-folder-demo-unzip-to.html
This comment has been removed by the author.
ReplyDeleteno errors found and everything is ok
ReplyDeletebut if i click on attachment button there is no response
manish can u help me and can i have your gmail id?
Are you using emulator or real device? In real device it must open the gallery.
Deleteno errors found and everything is ok
Deletebut if i click on attachment button there is no response
i am using on real device...gallery is open but if click the photo suddenly app is close
tell me the solutions
It is just because of your Uri is null inside onActivityResult(), its happen because of high resolution image. So try to Google for this issue.
DeleteHi Manish! Can you please send me the project in rar file? I really need this one for my school project and I am very new with this one. My email is alvin29jezreel@gmail.com , please consider my request. Thank you sir!
ReplyDeletePlease copy paste from above, I have lost my workspace.
DeleteThis comment has been removed by the author.
ReplyDeletegetting Error:Content is not allowed in trailing section.
ReplyDeletecan suggest anything that will not ask to choose application while sending email. It should automatically send it through email application in device.Thank you
ReplyDeleteI am not sure but user can set default app. Or you can direct open any app using their package name(Intent Filter). you need to do some R&D on it.
Deletethank you
Delete