Reputation: 15
I Wrote This Code To Send An Email But Give Me " No App Can Perform This action " Error ?!! Can Any One Help Me .!
public class MainActivity extends Activity {
Button startBtn;
Intent chooser = null , emailIntent = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
startBtn = (Button) findViewById(R.id.sendEmail);
startBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendEmail();
}
});
}
protected void sendEmail()
{
String[] TO = {"[email protected]"};
emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Thanks For Your Job");
emailIntent.setType("message/rfc822");
emailIntent.setPackage("com.Gmail");
chooser = Intent.createChooser(emailIntent, "Send Email");
startActivity(chooser);
}
}
but no emails Send
Upvotes: 0
Views: 69
Reputation: 1377
There are a couple of issues with your snippet.
emailIntent.setPackage("com.Gmail")
. This will only consider apps with the application ID "com.Gmail" as target for the intent. However, "com.Gmail" is not the application ID of the Gmail app. You shouldn't limit the selection anyway. So let's just drop this line.mailto
URI. One that includes the recipient, subject and text from your example looks like this: mailto:[email protected]?subject=Hello&body=Thanks%20For%20Your%20Job
Intent.createChooser()
you make them have to manually choose their email app every time (assuming there's more than one installed).Considering all this we end up with:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:[email protected]?subject=Hello&body=Thanks%20For%20Your%20Job"));
try {
startActivity(emailIntent);
} catch (ActivityNotFoundException e) {
//TODO: Display message that no email app was found
}
To make all this less painful I wrote the library EmailIntentBuilder. You can use it like this:
EmailIntentBuilder.from(context)
.to("[email protected]")
.subject("Hello")
.body("Thanks For Your Job")
.start();
I also wrote a blog post explaining all of this in more detail: Sending Email using Intents.
Upvotes: 0
Reputation: 818
You should use intent.setType("text/plain");
We can use "message/rfc822"
instead of "text/plain"
as the MIME type. However, that is not indicating "only offer email clients" -- it indicates "offer anything that supports message/rfc822 data". That could readily include some application that are not email clients.
message/rfc822 supports MIME Types of .mhtml, .mht, .mime
as alredy answered here: Send Email Intent
Upvotes: 1