Intent with setType(“message/rfc822”) for Android

2019-02-19 03:22发布

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 file doesn't have standart MIME-type (sqlite database) and I am trying to avoid a lot of applications in 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 of 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);

3条回答
甜甜的少女心
2楼-- · 2019-02-19 04:09

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.

查看更多
男人必须洒脱
3楼-- · 2019-02-19 04:11

This is the solution. Use the below code, works perfect...Got the solution after a research.... :)

Intent testIntent = new Intent(Intent.ACTION_VIEW);  

Uri data = Uri.parse("mailto:?subject=" + "blah blah subject" + "&body=" + "blah blah body" + "&to=" + "sendme@me.com");
testIntent.setData(data);
startActivity(testIntent);

查看更多
冷血范
4楼-- · 2019-02-19 04:14

i have made an application which uses uri example as u desried: my function has on click listener activated :

if(v.getId()==R.id.button3)
{
    intent=new Intent(Intent.ACTION_SEND);
    intent.setData(Uri.parse("mailto"));
    String[]to={"akshkatheria@gmail.com","megakatheria@gmail.com"};
    intent.putExtra(Intent.EXTRA_EMAIL, to);
    intent.putExtra(Intent.EXTRA_SUBJECT, "hello");
    intent.putExtra(Intent.EXTRA_TEXT, "hi");
    intent.setType("message/rfc822");
    chooser=intent.createChooser(intent, "send mail");
    startActivity(chooser);

}

查看更多
登录 后发表回答