Tuesday 14 October 2014

Send Mail Through Email Intent

In android While sending mail by Email intent You are using this code sample



Intent sendIntent=new Intent(); 
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,"Hello World");
sendIntent.setType("text/plain"); 
//startActivity(sendIntent);
startActivity(Intent.createChooser(sendIntent,"Restaurant Share to"));

you can use multiple type for the send multiple content like 


intent.setType("*/*"); //for all
intent.setType("text/html"); // for HTML content
intent.setType("image/*");//for image
intent.setType("text/plain");// for only text


While You are using this code so many intents are open in to the android device that contain the send intent.

But you want to open a specific intent in to android device that is contain the send intent that is also possible by this code.

Example If You r sending only mail then Use this following code


// for default intent
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_TEXT, "Hello World");
emailIntent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"xyz@gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT,  "Subject Enter Here");
emailIntent.setType("message/rfc822");

// get the list of intent and get total information that contan the send intent
PackageManager pm = getActivity().getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);  
sendIntent.setType("text/plain");

// for display message while opening the
Intent openInChooser = Intent.createChooser(emailIntent,"Send email...");

// then get the list of resolve information object list from package manager
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);

        List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();      

for (int i = 0; i < resInfo.size(); i++) {

// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
/*is the mail client available the check the condition which is available in to the list                               object*/
                        if(packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else if( packageName.contains("android.gm")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, i.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,"");
intent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"xyz@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject Enter here");                                 intent.setType("message/rfc822");
                                //add intent object in to the in to the lebeledIntent list
                                intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
                // convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);

No comments:

Post a Comment