How to send emails from my Android application?

前端 未结 21 2333
春和景丽
春和景丽 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:49

    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);
        }
    }
    

提交回复
热议问题