我正在尝试使用intent从我的应用程序发送电子邮件,但电子邮件的“收件人”字段不会填充。如果我添加代码来填写主题或文本,它们工作正常。只有To字段不会填充。
我也尝试将类型更改为“text / plain”和“text / html”,但我遇到了同样的问题。有人可以帮忙吗?
public void Email(){
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822"); //set the email recipient
String recipient = getString(R.string.IntegralEmailAddress);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL , recipient);
//let the user choose what email client to use
startActivity(Intent.createChooser(emailIntent, "Send mail using...")); }
我正在尝试使用的电子邮件客户端是Gmail
答案 0 :(得分:207)
我认为您没有将recipient
作为array of string
它应该像
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "someone@gmail.com" });
答案 1 :(得分:4)
使用此
:let new_list=map(copy(list), 'fnamemodify(v:val, ":p:t")')
这会起作用:) 这就是android文档中关于Intent.Extra_Email的说法 - 一个字符串数组全部" To"收件人电子邮件地址 所以你应该正确地输入字符串 你可以在这里阅读更多内容 http://developer.android.com/guide/components/intents-common.html#Email 在这里http://developer.android.com/guide/topics/resources/string-resource.html或者使用ACTION_SENDTO操作并包含" mailto:"数据方案。例如:
public void Email(){
// use this to declare your 'recipient' string and get your email recipient from your string xml file
Resources res = getResources();
String recipient = getString(R.string.IntegralEmailAddress);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822"); //set the email recipient
emailIntent.putExtra(Intent.EXTRA_EMAIL, recipient);
//let the user choose what email client to use
startActivity(Intent.createChooser(emailIntent, "Send mail using..."));
``}
答案 2 :(得分:2)
private void callSendMeMail() {
Intent Email = new Intent(Intent.ACTION_SEND);
Email.setType("text/email");
Email.putExtra(Intent.EXTRA_EMAIL, new String[] { "me@gmail.com" });
Email.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
startActivity(Intent.createChooser(Email, "Send mail to Developer:"));
}
答案 3 :(得分:0)
事物的结合:
1-您需要将操作常量变量设置为ACTION_SENDTO。
Intent intentEmail = new Intent(Intent.ACTION_SENDTO);
2-如果只希望通过邮件打开它,则使用setData()方法:intentEmail.setData(Uri.parse("mailto:"));
否则,它将要求您通过设备上存在的其他应用程序将其作为文本,图像,音频文件打开。 br />
3-您需要将电子邮件ID字符串作为数组对象传递,而不仅仅是字符串。字符串为:“ name@email.com” 。字符串的数组对象是: new String [] {“ email1”,“ email2”,“ more_email”} 。
intentEmail.putExtra(Intent.EXTRA_EMAIL, new String[] {"email@overflow.com", "abcd@stack.com"});
答案 4 :(得分:0)
在Kotlin中-Android
fun sendMail(
activity: Activity,
emailIds: Array<String>,
subject: String,
textMessage: String
) {
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.type = "text/plain"
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailIds)
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
emailIntent.putExtra(Intent.EXTRA_TEXT, textMessage)
emailIntent.setType("message/rfc822")
try {
activity.startActivity(
Intent.createChooser(
emailIntent,
"Send email using..."
)
)
} catch (ex: ActivityNotFoundException) {
Toast.makeText(
activity,
"No email clients installed.",
Toast.LENGTH_SHORT
).show()
}
}
您还可以使用[
val emailIntent = Intent(Intent.ACTION_SENDTO)
]调用直接电子邮件 客户
//argument of function
val subject = "subject of you email"
val eMailMessageTxt = "Add Message here"
val eMailId1 = "emailId1@gmail.com"
val eMailId2 = "emailId2@gmail.com"
val eMailIds: Array<String> = arrayOf(eMailId1,eMailId2)
//Calling function
sendMail(this, eMailIds, subject, eMailMessageTxt)
我希望此代码段对kotlin开发人员有所帮助。
答案 5 :(得分:0)
这对我有用:
val email = "recipient@email.com"
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:$email")
intent.putExtra(Intent.EXTRA_SUBJECT,"My Subject")
startActivity(intent)
答案 6 :(得分:0)
让我浪费了这么多时间!感谢接受的答案! 我将添加一些 Kotlin 代码和一些方便的扩展函数
fun Activity.hasEmailClient(): Boolean =
emailIntent("someAddress", "someSubject", "someText")
.resolveActivity(packageManager) != null
fun Activity.openEmailClient(address: String, subject: String, text: String) {
startActivity(emailIntent(address, subject, text))
}
private fun emailIntent(address: String, subject: String, text: String): Intent =
Intent(Intent.ACTION_SENDTO).apply {
type = "text/plain"
data = Uri.parse("mailto:")
putExtra(Intent.EXTRA_EMAIL, arrayOf(address))
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT, text)
}
如果它适合您的需要,请随意将 Activity 替换为 Fragment。示例用法:
if (hasEmailClient()) {
openEmailClient(
"example@email.info",
"this is the subject",
"this is the text"
)
}