How to code using android studio to send an email

前端 未结 3 989
渐次进展
渐次进展 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);
      }
      

      }

    0 讨论(0)
  • 2021-02-04 07:39

    If you use Intent.ACTION_SEND android show all communicatons app. If you want show only email client you can use the following code.

     Intent mailIntent = new Intent(Intent.ACTION_VIEW);
     Uri data = Uri.parse("mailto:?subject=" + "subject text"+ "&body=" + "body text " + "&to=" + "destination@mail.com");
     mailIntent.setData(data);
     startActivity(Intent.createChooser(mailIntent, "Send mail..."));
    
    0 讨论(0)
  • 2021-02-04 07:47

    If you want to send email in background refer here

    If user is waiting on screen use below method:

    protected void sendEmail() {
          Log.i("Send email", "");
    
          String[] TO = {"someone@gmail.com"};
          String[] CC = {"xyz@gmail.com"};
          Intent emailIntent = new Intent(Intent.ACTION_SEND);
          emailIntent.setData(Uri.parse("mailto:"));
          emailIntent.setType("text/plain");
    
    
          emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
          emailIntent.putExtra(Intent.EXTRA_CC, CC);
          emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
          emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
    
          try {
             startActivity(Intent.createChooser(emailIntent, "Send mail..."));
             finish();
             Log.i("Finished sending email...", "");
          } catch (android.content.ActivityNotFoundException ex) {
             Toast.makeText(MainActivity.this, 
             "There is no email client installed.", Toast.LENGTH_SHORT).show();
          }
       }
    
    0 讨论(0)
提交回复
热议问题