Sunday, September 25, 2011

Sending Multiple file attached email in android

Sending Multiple file attached email in android  ,

public boolean sendEmailWithMultipleAttachments(Context context,
	String[] emailTo, String[] emailCC, String[] emailBCC,
	String subject, String emailBody, List filePaths) throws ActivityNotFoundException {
final Intent emailIntent = 
            new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailTo);
emailIntent.putExtra(android.content.Intent.EXTRA_CC, emailCC);
emailIntent.putExtra(android.content.Intent.EXTRA_BCC, emailBCC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody);

if (filePaths != null) {
         // has to be an ArrayList
	ArrayList uris = new ArrayList();
	// convert from paths to Android friendly Parcelable Uri's
	for (String file : filePaths) {
		File fileIn = new File(file);
			if (fileIn.exists()) {
				Uri u = Uri.fromFile(fileIn);
                                         (Intent.EXTRA_STREAM, uris);
	context.startActivity(Intent.createChooser(emailIntent, "Sent mail"));
	return true;

filePaths is list, which store the local path of file which we need to attach

Get mime type of a file

In android to get mime type is very easy...

To find the mime type of a file, path

String extention =  path.substring(path.lastIndexOf(.) );
String mimeTypeMap =MimeTypeMap.getFileExtensionFromUrl(extention);
String mimeType = MimeTypeMap.getSingleton()