How to send emails from my Android application?

前端 未结 21 2241
春和景丽
春和景丽 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 used this code to send mail by launching default mail app compose section directly.

        Intent i = new Intent(Intent.ACTION_SENDTO);
        i.setType("message/rfc822"); 
        i.setData(Uri.parse("mailto:"));
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"test@gmail.com"});
        i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        i.putExtra(Intent.EXTRA_TEXT   , "body of email");
        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }
    
    0 讨论(0)
  • 2020-11-22 00:50

    To JUST LET EMAIL APPS to resolve your intent you need to specify ACTION_SENDTO as Action and mailto as Data.

    private void sendEmail(){
    
        Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
        emailIntent.setData(Uri.parse("mailto:" + "recipient@example.com")); 
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My email's subject");
        emailIntent.putExtra(Intent.EXTRA_TEXT, "My email's body");
    
        try {
            startActivity(Intent.createChooser(emailIntent, "Send email using..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(Activity.this, "No email clients installed.", Toast.LENGTH_SHORT).show();
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 00:51

    Use .setType("message/rfc822") or the chooser will show you all of the (many) applications that support the send intent.

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

    This is how I did it. Nice and simple.

    String emailUrl = "mailto:email@example.com?subject=Subject Text&body=Body Text";
            Intent request = new Intent(Intent.ACTION_VIEW);
            request.setData(Uri.parse(emailUrl));
            startActivity(request);
    
    0 讨论(0)
  • 2020-11-22 00:54

    Here is the sample working code which opens mail application in android device and auto-filled with To address and Subject in the composing mail.

    protected void sendEmail() {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:feedback@gmail.com"));
        intent.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 00:54

    This function first direct intent gmail for sending email, if gmail is not found then promote intent chooser. I used this function in many commercial app and it's working fine. Hope it will help you:

    public static void sentEmail(Context mContext, String[] addresses, String subject, String body) {
    
        try {
            Intent sendIntentGmail = new Intent(Intent.ACTION_VIEW);
            sendIntentGmail.setType("plain/text");
            sendIntentGmail.setData(Uri.parse(TextUtils.join(",", addresses)));
            sendIntentGmail.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
            sendIntentGmail.putExtra(Intent.EXTRA_EMAIL, addresses);
            if (subject != null) sendIntentGmail.putExtra(Intent.EXTRA_SUBJECT, subject);
            if (body != null) sendIntentGmail.putExtra(Intent.EXTRA_TEXT, body);
            mContext.startActivity(sendIntentGmail);
        } catch (Exception e) {
            //When Gmail App is not installed or disable
            Intent sendIntentIfGmailFail = new Intent(Intent.ACTION_SEND);
            sendIntentIfGmailFail.setType("*/*");
            sendIntentIfGmailFail.putExtra(Intent.EXTRA_EMAIL, addresses);
            if (subject != null) sendIntentIfGmailFail.putExtra(Intent.EXTRA_SUBJECT, subject);
            if (body != null) sendIntentIfGmailFail.putExtra(Intent.EXTRA_TEXT, body);
            if (sendIntentIfGmailFail.resolveActivity(mContext.getPackageManager()) != null) {
                mContext.startActivity(sendIntentIfGmailFail);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题