How can I send emails from my Android application?

2018-12-31 03:40发布

I am writing an application for Android. How can I send an email from it?

标签: android email
19条回答
一个人的天荒地老
2楼-- · 2018-12-31 03:41

Use .setType("message/rfc822") or the chooser will show you all of the (many) applications that support the send intent.

查看更多
春风洒进眼中
3楼-- · 2018-12-31 03:41

The strategy of using .setType("message/rfc822") or ACTION_SEND seems to also match apps that aren't email clients, such as Android Beam and Bluetooth.

Using ACTION_SENDTO and a mailto: URI seems to work perfectly, and is recommended in the developer documentation. However, if you do this on the official emulators and there aren't any email accounts set up (or there aren't any mail clients), you get the following error:

Unsupported action

That action is not currently supported.

As shown below:

Unsupported action: That action is not currently supported.

It turns out that the emulators resolve the intent to an activity called com.android.fallback.Fallback, which displays the above message. Apparently this is by design.

If you want your app to circumvent this so it also works correctly on the official emulators, you can check for it before trying to send the email:

private void sendEmail() {
    Intent intent = new Intent(Intent.ACTION_SENDTO)
        .setData(new Uri.Builder().scheme("mailto").build())
        .putExtra(Intent.EXTRA_EMAIL, new String[]{ "John Smith <johnsmith@yourdomain.com>" })
        .putExtra(Intent.EXTRA_SUBJECT, "Email subject")
        .putExtra(Intent.EXTRA_TEXT, "Email body")
    ;

    ComponentName emailApp = intent.resolveActivity(getPackageManager());
    ComponentName unsupportedAction = ComponentName.unflattenFromString("com.android.fallback/.Fallback");
    if (emailApp != null && !emailApp.equals(unsupportedAction))
        try {
            // Needed to customise the chooser dialog title since it might default to "Share with"
            // Note that the chooser will still be skipped if only one app is matched
            Intent chooser = Intent.createChooser(intent, "Send email with");
            startActivity(chooser);
            return;
        }
        catch (ActivityNotFoundException ignored) {
        }

    Toast
        .makeText(this, "Couldn't find an email app and account", Toast.LENGTH_LONG)
        .show();
}

Find more info in the developer documentation.

查看更多
不再属于我。
4楼-- · 2018-12-31 03:41

Sending email can be done with Intents which will require no configuration. But then it will require user interaction and the layout will be a bit restricted.

Build and sending a more complex email without user interaction entails building your own client. The first thing is that the Sun Java API for email are unavailable. I have had success leveraging the Apache Mime4j library to build email. All based on the docs at nilvec.

查看更多
呛了眼睛熬了心
5楼-- · 2018-12-31 03:42

I've been using this since long time ago and it seems good, no non-email apps showing up. Just another way to send a send email intent:

Intent intent = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of email");
intent.putExtra(Intent.EXTRA_TEXT, "Body of email");
intent.setData(Uri.parse("mailto:default@recipient.com")); // or just "mailto:" for blank
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
startActivity(intent);
查看更多
皆成旧梦
6楼-- · 2018-12-31 03:45

This is how I did it. Nice and simple.

String emailUrl = "mailto:email@example.com?subject=Subject Text&body=Body Text";
        Intent request = new Intent(Intent.ACTION_VIEW);
        request.setData(Uri.parse(emailUrl));
        startActivity(request);
查看更多
栀子花@的思念
7楼-- · 2018-12-31 03:46

I solved this issue with simple lines of code as the android documentation explains.

(https://developer.android.com/guide/components/intents-common.html#Email)

The most important is the flag: it is ACTION_SENDTO, and not ACTION_SEND

The other important line is

intent.setData(Uri.parse("mailto:")); ***// only email apps should handle this***

By the way, if you send an empty Extra, the if() at the end won't work and the app won't launch the email client.

According to Android documentation. If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the ACTION_SENDTO action and include the "mailto:" data scheme. For example:

public void composeEmail(String[] addresses, String subject) {
    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:")); // only email apps should handle this
    intent.putExtra(Intent.EXTRA_EMAIL, addresses);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}
查看更多
登录 后发表回答