In this tutorial you will learn about how to send programmatically emails in Android and how to attach files, images or videos.
Email is used for professional and personal conversation very commonly. Many applications require email option to send data to others. You can easily send emails with your Android applications. What about attachments? Unfortunately Android does not allow us to attach many different file formats but you can add few of them like photos, audios, videos, pdf and word documents.
Example of Email Attachment
Lets first create an app which allow users to send data through email. Intent.ACTION_SEND is used to send emails through an existing clients in your cell phone. Similarly Intent.ACTION_GET_CONTENT is used for attachments.
Intent.ACTION_SEND
ACTION_SEND is used to send data to other activities in your app or to other apps. Both are easy to understand and work with. If you want to send data to other activity in your app then all you need is to specify the data and its type. Tha Android OS will do the rest… it will find compatible activity and display the data. Similarly you can send data to other applications. Set type of content to simple plain text. Here is how it should be coded
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);
Intent.ACTION_GET_CONTENT
ACTION_GET_CONTENT is used to select multiple types of data and return it. You can directly pick what type of content you want by setting MIME type to that kind of data. Or you can pick any type of data you want. For this wrap your intent with a chooser, which will start a new activity and let the user to pick their desired content. Here is how to do it
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);
Add Permission to manifest file
Add following permissions to your manifest file.
<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" />
Open your Android Studio and create a new project. Create main activity and paste the following code in your activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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/et_to" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_margin="5dp" android:hint="Receiver's Email Address!" android:inputType="textEmailAddress" android:singleLine="true" /> <EditText android:id="@+id/et_subject" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/et_to" android:layout_margin="5dp" android:hint="Enter Subject" android:singleLine="true" /> <EditText android:id="@+id/et_message" android:layout_width="match_parent" android:layout_height="200dp" android:layout_below="@id/et_subject" android:layout_margin="5dp" android:gravity="top|left" android:hint="Compose Email" android:inputType="textMultiLine" /> <Button android:id="@+id/bt_send" android:layout_width="80dp" android:layout_height="50dp" android:layout_below="@id/et_message" android:layout_margin="5dp" android:text="Send" /> <Button android:id="@+id/bt_attachment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="attachment" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> </ScrollView>
Now here is MainActivity.java
package com.example.admin.emailattachmentexample; 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.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { EditText et_email; EditText et_subject; EditText et_message; Button Send; Button Attachment; String email; String subject; String message; String 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); et_email = (EditText) findViewById(R.id.et_to); et_subject = (EditText) findViewById(R.id.et_subject); et_message = (EditText) findViewById(R.id.et_message); Attachment = (Button) findViewById(R.id.bt_attachment); Send = (Button) findViewById(R.id.bt_send); //send button listener Send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendEmail(); } }); //attachment button listener Attachment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openFolder(); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PICK_FROM_GALLERY && 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]); attachmentFile = cursor.getString(columnIndex); Log.e("Attachment Path:", attachmentFile); URI = Uri.parse("file://" + attachmentFile); cursor.close(); } } public void sendEmail() { try { email = et_email.getText().toString(); subject = et_subject.getText().toString(); message = et_message.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 openFolder() { 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); } }
Run your app and here is the output
Click on add Attachment
Now click on send button
You can download this project by clicking this link.
References
Android ACTION_GET_CONTENT javadoc
View Comments (3)
hi why I cannot open it with android studio?
Attachment is not working .
For that, you normally open the mail and Find the sending page and from there you will find an option attachment and after that, you will share your file by it.