Send Email Intent

前端 未结 30 3813
忘掉有多难
忘掉有多难 2020-11-22 07:27
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(\"text/html\");
intent.putExtra(Intent.EXTRA_EMAIL, \"emailaddress@emailaddress.com\");
intent.putExtr         


        
30条回答
  •  面向向阳花
    2020-11-22 08:00

    There are three main approaches:

    String email = /* Your email address here */
    String subject = /* Your subject here */
    String body = /* Your body here */
    String chooserTitle = /* Your chooser title here */
    

    1. Custom Uri:

    Uri uri = Uri.parse("mailto:" + email)
        .buildUpon()
        .appendQueryParameter("subject", subject)
        .appendQueryParameter("body", body)
        .build();
    
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
    startActivity(Intent.createChooser(emailIntent, chooserTitle));
    

    2. Using Intent extras:

    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, body);
    //emailIntent.putExtra(Intent.EXTRA_HTML_TEXT, body); //If you are using HTML in your body text
    
    startActivity(Intent.createChooser(emailIntent, "Chooser Title"));
    

    3. Support Library ShareCompat:

    Activity activity = /* Your activity here */
    
    ShareCompat.IntentBuilder.from(activity)
        .setType("message/rfc822")
        .addEmailTo(email)
        .setSubject(subject)
        .setText(body)
        //.setHtmlText(body) //If you are using HTML in your body text
        .setChooserTitle(chooserTitle)
        .startChooser();
    

提交回复
热议问题