Sending Email from Android app when click on button

后端 未结 2 1097
野趣味
野趣味 2021-01-01 12:03

I need to provide feature for users where users can share some data by sending email. I used below code.

    Intent email = new Intent(android.content.Intent         


        
相关标签:
2条回答
  • 2021-01-01 12:56

    Try this:

    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                "mailto","email@email.com", null));
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_TEXT, message);
    startActivity(Intent.createChooser(intent, "Choose an Email client :"));
    

    If you don't have a specific recipient - go like this:

    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                "mailto", "", null));
    
    0 讨论(0)
  • 2021-01-01 13:02

    use this method to share via gmail only jus you need to call

    startActivity(getSendEmailIntent(context, email,subject, body));
    
    
    
    
    
    public Intent getSendEmailIntent(Context context, String email,
                        String subject, String body) {
                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    
                try {
    
                    // Explicitly only use Gmail to send
                    emailIntent.setClassName("com.google.android.gm",
                            "com.google.android.gm.ComposeActivityGmail");
    
                    emailIntent.setType("text/html");
    
                    // Add the recipients
                    if (email != null)
                        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                                new String[] { email });
    
                    if (subject != null)
                        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                                subject);
    
                    if (body != null)
                        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body));
    
                    // Add the attachment by specifying a reference to our custom
                    // ContentProvider
                    // and the specific file of interest
                    // emailIntent.putExtra(
                    // Intent.EXTRA_STREAM,
                    // Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"
                    // + fileName));
    
                     return emailIntent;
        //          myContext.startActivity(emailIntent);
                } catch (Exception e) {
                    emailIntent.setType("text/html");
    
                    // Add the recipients
                    if (email != null)
                        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                                new String[] { email });
    
                    if (subject != null)
                        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                                subject);
    
                    if (body != null)
                        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body));
    
        //          myContext.startActivity(Intent.createChooser(emailIntent,
        //                  "Share Via"));
                     return emailIntent;
                }
                }
    
    0 讨论(0)
提交回复
热议问题