How to code using android studio to send an email

前端 未结 3 988
渐次进展
渐次进展 2021-02-04 07:23

I\'m planning to develop an android mobile application using android studio, where an user give email address and secret code. Then that secret code should be send to mentioned

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 07:37

    https://developer.android.com/guide/components/intents-common#ComposeEmail

    1. Only email apps

      public void composeEmail(String[] addresses, String subject, Uri attachment) {
      Intent intent = new Intent(Intent.ACTION_SEND);
      intent.setType("*/*");
      intent.putExtra(Intent.EXTRA_EMAIL, addresses);
      intent.putExtra(Intent.EXTRA_SUBJECT, subject);
      intent.putExtra(Intent.EXTRA_STREAM, attachment);
      if (intent.resolveActivity(getPackageManager()) != null) {
          startActivity(intent);
      }
      

      }

    2. Any messaging app:

      public void composeEmail(String[] addresses) {
      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, "");
      if (intent.resolveActivity(getPackageManager()) != null) {
          startActivity(intent);
      }
      

      }

提交回复
热议问题