7

I am writing an app in which i am trying to send an email with some data, but whenever i do click on Submit button to send an email, getting : Unfortunately App has Stopped

Error:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND typ=text/plain (has extras) }

Code:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("text/plain");
String aEmailList[] = { "myaccount@gmail.com" };  
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);   
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body.toString()));
startActivity(emailIntent);

Logcat:

08-01 08:34:22.518: E/AndroidRuntime(1043): FATAL EXCEPTION: main
08-01 08:34:22.518: E/AndroidRuntime(1043): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO typ=text/plain (has extras) }
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.Activity.startActivityForResult(Activity.java:3370)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.Activity.startActivityForResult(Activity.java:3331)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.Activity.startActivity(Activity.java:3566)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.Activity.startActivity(Activity.java:3534)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at app.my.BookAppointmentActivity$6.onClick(BookAppointmentActivity.java:206)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.view.View.performClick(View.java:4204)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.view.View$PerformClick.run(View.java:17355)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.os.Handler.handleCallback(Handler.java:725)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.os.Looper.loop(Looper.java:137)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at java.lang.reflect.Method.invoke(Method.java:511)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 08:34:22.518: E/AndroidRuntime(1043):     at dalvik.system.NativeStart.main(Native Method)
Sneha
  • 1,051
  • 4
  • 14
  • 23

3 Answers3

8

Few months ago i was facing same problem, and i found a small solution, please try below code by replacing yours

Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("plain/text");  
    i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient@gmail.com"});
    i.putExtra(Intent.EXTRA_SUBJECT, subject);
    i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body.toString()));
    try {
     startActivity(i);
    } catch (android.content.ActivityNotFoundException ex) {
     Toast.makeText(Activity.this, "There are no email applications installed.", Toast.LENGTH_SHORT).show();
    }
}

If you will get There are no email applications installed. message, it means your work is done and i will suggest you to check it on real DEVICE, but i also don't know how to send it via EMULATOR

Android
  • 1,529
  • 1
  • 12
  • 27
4

The Android emulator seems to lack a configured email account. That's why your code crashes. I'd suggest catching an ActivityNotFoundException when trying to send:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("text/plain");
String aEmailList[] = { "myaccount@gmail.com" };  
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);   
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body.toString()));
try{    
  startActivity(emailIntent);
} catch (ActivityNotFoundException ex){
  Toast.makeText(this, "No activity found", Toast.LENGTH_LONG).show(); //Display an error message
}

You can get more information on this issue here.

Especially this paragraph is interesting:

If you are using an emulator, you’ll need to configure the email client. If the email client is not configured, it will not respond to the Intent we’ll be discussing. If you want to see the chooser in action, you’ll need to configure a device using multiple messaging applications, such as the Gmail application and the Email application.

FD_
  • 12,947
  • 4
  • 35
  • 62
  • From your LogCat, it seems you are using android.intent.action.SENDTO. That one requires you to specify recipients and subject via an url scheme, whereas android.content.Intent.ACTION_SEND (as you use it in your posted code ?!) takes these parameters as extras. See this for more info: http://stackoverflow.com/a/8284804/1691231 – FD_ Aug 01 '13 at 09:01
  • it opens new email to use, the blank email – Sneha Aug 01 '13 at 09:07
  • Which action are you using for your `Intent` ? – FD_ Aug 01 '13 at 09:08
  • how to send email via Emulator – Sneha Aug 01 '13 at 09:19
  • From the text above: `you’ll need to configure the email client. If the email client is not configured, it will not respond to the Intent` – FD_ Aug 01 '13 at 09:25
  • i have also clicked on useful for your answer – Sneha Aug 01 '13 at 09:34
4

Instead of using Try-Catch block you can use resolveActivity() method on your Intent object. If the result is non-null, then there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, you should not use the intent and, if possible, you should disable the feature that issues the intent.

You can use below solution:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("text/plain");
String aEmailList[] = { "myaccount@gmail.com" };  
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);   
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body.toString()));

// Verify that the intent will resolve to an activity
if (emailIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
}else{
    Toast.makeText(this, "No activity found to send mail.", Toast.LENGTH_LONG).show(); 
}

You can find more details here.

I hope it helps.

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78