英文:
Android app email recipient option not functioning
问题
我正在使用以下示例尝试向特定收件人发送电子邮件。
https://developer.android.com/guide/components/intents-common.html#Email
电子邮件客户端弹出,但“收件人”字段为空... 有什么想法吗?
根据上述网页中的文档,我已经提到EXTRA_EMAIL填写了收件人的电子邮件地址。
Intent.EXTRA_EMAIL
一个包含所有“收件人”电子邮件地址的字符串数组。
以下是代码:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // 仅电子邮件应用程序应处理此操作
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"testemail@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "电子邮件标题");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
英文:
I am using the following example to try to send an email to a specific recipient.
https://developer.android.com/guide/components/intents-common.html#Email
The email client pops up but To: field is empty...any thoughts?
According to the documentation in the above webpage i have mentioned EXTRA_EMAIL fills the recipient email address
Intent.EXTRA_EMAIL
A string array of all "To" recipient email addresses.
Here is the code
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, "testemail@gmail.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "Title of the email");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
答案1
得分: 3
为了处理,应该是一个字符串数组:
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "me@somewhere.com" });
英文:
To address should be a String Array:
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "me@somewhere.com" });
答案2
得分: 1
请注意,ACTION_SENDTO
本身未记录支持 EXTRA_EMAIL
或 EXTRA_SUBJECT
。依我之见,更安全的方法是正确使用 mailto:
:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:testemail@gmail.com"));
英文:
Note that ACTION_SENDTO
itself is not documented to support EXTRA_EMAIL
or EXTRA_SUBJECT
. IMHO, the safer approach is to use mailto:
properly:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:testemail@gmail.com"));
答案3
得分: 0
这是我翻译好的部分:
正如文件和其他回答中所述,地址应为数组。以下是我用于组合电子邮件的 Helper 类:
public class EmailComposer {
private Activity activity;
private String subject;
private String[] addresses;
public EmailComposer(Activity activity, String subject, String[] addresses) {
this.activity = activity;
this.subject = subject;
this.addresses = addresses;
}
public void composeEmail() {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // 仅电子邮件应用程序应处理此项
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (activity != null) {
if (intent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivity(intent);
}
}
}
}
请注意,上述代码可能需要在正确的上下文和环境中运行。
英文:
As its stated in the document and other answer the address should be array. This the Helper class I am using for composing e-mail
public class EmailComposer {
private Activity activity;
private String subject;
private String[] addresses;
public EmailComposer(Activity activity, String subject, String[] addresses) {
this.activity = activity;
this.subject = subject;
this.addresses = addresses;
}
public void composeEmail() {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if(activity!=null) {
if (intent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivity(intent);
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论