简体   繁体   English

通过 Android 发送 Email 意图

[英]send Email through Android Intent

I have tried to send Email through Android Intent by using below code我尝试使用以下代码通过 Android Intent 发送 Email

    Intent sendIntent = new Intent();

    sendIntent.putExtra(Intent.EXTRA_TEXT, EmailContent);
    sendIntent.putExtra(Intent.EXTRA_EMAIL, RecipientName );
    sendIntent.putExtra(Intent.EXTRA_SUBJECT , subject );
    sendIntent.setType("message/rfc822");
    sendIntent.setAction(Intent.ACTION_SEND);

    Intent chooser = Intent.createChooser(sendIntent , chooser_title );

    if (sendIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(chooser);
    }

In the email app the recipient details are not getting update whereas all the other details such as subject, body is getting updated with my input.在 email 应用程序中,收件人详细信息没有更新,而主题、正文等所有其他详细信息都随着我的输入而更新。 Could you please suggest what needs to be done to resolve this.您能否建议需要做些什么来解决这个问题。

Try to pass the EXTRA_EMAIL as string array.尝试将 EXTRA_EMAIL 作为字符串数组传递。

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
            "mailto","abc@gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(emailIntent, "Send email..."));

Try using this:- Create String resource for recipients:-尝试使用这个:- 为收件人创建字符串资源:-

    <string-array name="receipients">
    <item>mgf@kgf.co</item>
    <item>sdf@kgf.co</item>
</string-array>

and use this intent并使用此意图

     Intent gmailIntent = new Intent(Intent.ACTION_SEND);
        gmailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        gmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, resources.getStringArray(R.array.receipients));
        gmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
        gmailIntent.putExtra(android.content.Intent.EXTRA_TEXT, EmailContent);
        gmailIntent.setType("message/rfc822");
            gmailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
        final PackageManager pm = context.getApplicationContext().getPackageManager();
        final List<ResolveInfo> matches = pm.queryIntentActivities(gmailIntent, 0);
        ResolveInfo best = null;
        for (final ResolveInfo info : matches)
            if (info.activityInfo.packageName.endsWith(".gm") ||
                    info.activityInfo.name.toLowerCase().contains("gmail")) best = info;
        if (best != null)
            gmailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);


        try {
           startActivity(gmailIntent);
        } catch (ActivityNotFoundException ex) {
            Toast.makeText(context.getApplicationContext(), getString(R.string.you_do_not_have_gmail_installed), Toast.LENGTH_SHORT).show();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM