Search code examples
androidemailandroid-intentandroid-emulator

Intent with setType("message/rfc822") for Android API Level before 2.3.3


I have a problem with setting type "message/rfc822" for intent to send e-mail with file attachment on Android emulator. I have to use setType("message/rfc822") because the file doesn't have standard MIME-type (SQLite database) and I am trying to avoid a lot of applications in the select list for user's choice. For all API Levels before 2.3.3 I have an error:

java.lang.RuntimeException: 
Unable to start activity ComponentInfo{my.cashwatcher/my.cashwatcher.SendEmailActivity}: 
android.content.ActivityNotFoundException: 
No Activity found to handle Intent { act=android.intent.action.SEND typ=message/rfc822 
(has extras) }

In the case of API Level, 2.3.3 code works fine and error doesn't appear. Is it a problem with the Android emulator or old APIs!?

Code:

Intent sendIntent = new Intent(Intent.ACTION_SEND);                         
sendIntent.setType("message/rfc822");            
sendIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{appPrefs.getEmail("email")});                   
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), DATABASE_PATH)));
sendIntent.putExtra(Intent.EXTRA_TEXT, "body_of_email"); 
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "APPLICATION_NAME");
startActivityForResult(sendIntent, EMAIL_SEND_RESULT);

Solution

  • First, "to avoid a lot of applications in select list for user's choice", use ACTION_SENDTO and a mailto: Uri.

    Second, what you are experiencing is not "a problem of Android emulator" nor "old APIs". You need 1+ applications that are capable of handling the ACTION_SEND Intent and a MIME type of message/rfc822. There is no guarantee that any given device will support that combination, let alone any given emulator. Your code needs to handle that, just as if you use ACTION_GOBBLEDYGOOK or a MIME type of thisis/sonotreal or whatever.