Send mail with attachment on Emulator – Android

02 Aug

Most of the developers facing the issue of attaching files on mail client present on Emulator.
To resolve this issue need to add the code shown below:

private boolean sendMailWithAttachment(Intent objIntent){
boolean blnMailSentSuccessfully = false;
ArrayList<Uri> uris;
String[] filePaths;
File fileIn = null;
//To attach multiple files use action as ACTION_SEND_MULTIPLE
objIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
objIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{Your To Recipient List});
objIntent.putExtra(android.content.Intent.EXTRA_CC, "");
objIntent.putExtra(android.content.Intent.EXTRA_BCC, "");
objIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Your Subject");

//Attach multiple files to the mail client
uris = new ArrayList<Uri>();
filePaths = new String[] {File paths to attach};
for (String file : filePaths){
fileIn = new File(file);

First Option:

ContentValues values = new ContentValues();
//values.put(Images.Media.TITLE, fileIn.getName());
//values.put(Images.Media.DISPLAY_NAME, fileIn.getName());
values.put(Images.Media.MIME_TYPE, "text/plain");

//values.put(Images.Media.DATE_TAKEN, new Date().getTime());
//values.put(Images.ImageColumns.BUCKET_ID, fileIn.getAbsolutePath().hashCode());
//values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, fileIn.getName());
ContentResolver contentResolver = getApplicationContext().getContentResolver();
Uri uri = contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);

Alternative Option:

/*Uri u = Uri.fromFile(fileIn);
objIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
objIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Your Mail Body Content");
blnMailSentSuccessfully = true;
catch (Exception e){
objIntent = null;
uris = null;
filePaths = null;
fileIn = null;
return blnMailSentSuccessfully;

Note: Always check this functionality is working on Emulator. If it works fine then your code can run on any of the device and user will not face any issues related to this.

Leave a comment

Posted by on August 2, 2012 in Android Source Code


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: