首页 > 解决方案 > 我们如何在从应用程序传递电子邮件意图时动态设置来自电子邮件

问题描述

我想通过我的应用程序中的电子邮件意图发送文件。但我想根据我的选择通过代码设置电子邮件 FROM id,而不是默认情况下在我的设备中使用 gmail 登录。Android 代码可以吗?

public void sendEmail() {
        try {
            Uri photoURI = FileProvider.getUriForFile(UserDetails.this, getApplicationContext().getPackageName() + ".provider", jsonFile);
            String email = user.getEmail();
            String subject = "User Export Data";
            String message = "For getting the better understanding.Please open this json file with the json viewer tools.";
            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("plain/text");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{email});
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
            if (jsonFile != null) {
                emailIntent.putExtra(Intent.EXTRA_STREAM, photoURI);
            }
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
            this.startActivity(emailIntent);
//            this.startActivity(Intent.createChooser(emailIntent, "Sending email..."));
        } catch (Throwable t) {
            Toast.makeText(this, "Request failed try again: " + t.toString(), Toast.LENGTH_LONG).show();
        }
    }

标签: javaandroid

解决方案


尝试这个

this.startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:to@gmail.com")));

或者

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                          emailIntent.setType("text/plain");
                          emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String\[\]{  "serveroverloadofficial@gmail.com"});
                          emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hello There");
                          emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Add Message here");


                            emailIntent.setType("message/rfc822");

                        try {
                            startActivity(Intent.createChooser(emailIntent,
                                    "Send email using..."));
                        } catch (android.content.ActivityNotFoundException ex) {
                            Toast.makeText(getActivity(),
                                    "No email clients installed.",
                                    Toast.LENGTH_SHORT).show();
                        }

                    }
                });

推荐阅读