How to send emails from my Android application?

前端 未结 21 2239
春和景丽
春和景丽 2020-11-22 00:38

I am developing an application in Android. I don\'t know how to send an email from the application?

相关标签:
21条回答
  • 2020-11-22 00:57

    This will show you only the email clients (as well as PayPal for some unknown reason)

     public void composeEmail() {
    
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:"));
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"hi@example.com"});
        intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        intent.putExtra(Intent.EXTRA_TEXT, "Body");
        try {
            startActivity(Intent.createChooser(intent, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 00:58

    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.

    0 讨论(0)
  • 2020-11-22 01:00

    The best (and easiest) way is to use an Intent:

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
    i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
    i.putExtra(Intent.EXTRA_TEXT   , "body of email");
    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }
    

    Otherwise you'll have to write your own client.

    0 讨论(0)
  • 2020-11-22 01:02

    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.

    0 讨论(0)
  • 2020-11-22 01:02
     Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                "mailto","ebgsoldier@gmail.com", null));
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Forgot Password");
        emailIntent.putExtra(Intent.EXTRA_TEXT, "Write your Pubg user name or Phone Number");
        startActivity(Intent.createChooser(emailIntent, "Send email..."));**strong text**
    
    0 讨论(0)
  • 2020-11-22 01:03

    This method work for me. It open Gmail app (if installed) and set mailto.

    public void openGmail(Activity activity) {
        Intent emailIntent = new Intent(Intent.ACTION_VIEW);
        emailIntent.setType("text/plain");
        emailIntent.setType("message/rfc822");
        emailIntent.setData(Uri.parse("mailto:"+activity.getString(R.string.mail_to)));
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, activity.getString(R.string.app_name) + " - info ");
        final PackageManager pm = activity.getPackageManager();
        final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
        ResolveInfo best = null;
        for (final ResolveInfo info : matches)
            if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail"))
                best = info;
        if (best != null)
            emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
        activity.startActivity(emailIntent);
    }
    
    0 讨论(0)
提交回复
热议问题