dir*_*ira 549 email android android-intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, "emailaddress@emailaddress.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");
startActivity(Intent.createChooser(intent, "Send Email"));
Run Code Online (Sandbox Code Playgroud)
上面的代码打开一个对话框,显示以下应用程序: - 蓝牙,Google Docs,Yahoo Mail,Gmail,Orkut,Skype等.
实际上,我想过滤这些列表选项.我想只显示与电子邮件相关的应用程序,例如Gmail,Yahoo Mail.怎么做?
我在"Android Market"应用程序上看到了这样的例子.
该对话框仅显示电子邮件应用程序,例如Gmail,Yahoo Mail等.它不显示蓝牙,Orkut等.什么代码产生这样的对话?
tha*_*h84 848
接受的答案不适用于4.1.2.这适用于所有平台:
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..."));
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
更新:根据marcwjj,似乎在4.3,我们需要传递字符串数组而不是字符串的电子邮件地址,以使其工作.我们可能需要再添加一行:
intent.putExtra(Intent.EXTRA_EMAIL, addresses); // String[] addresses
Run Code Online (Sandbox Code Playgroud)
dir*_*ira 225
主要有三种方法:
String email = /* Your email address here */
String subject = /* Your subject here */
String body = /* Your body here */
String chooserTitle = /* Your chooser title here */
Run Code Online (Sandbox Code Playgroud)
1.定制Uri:
Uri uri = Uri.parse("mailto:" + email)
.buildUpon()
.appendQueryParameter("subject", subject)
.appendQueryParameter("body", body)
.build();
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(Intent.createChooser(emailIntent, chooserTitle));
Run Code Online (Sandbox Code Playgroud)
2.使用Intent附加功能:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
//emailIntent.putExtra(Intent.EXTRA_HTML_TEXT, body); //If you are using HTML in your body text
startActivity(Intent.createChooser(emailIntent, "Chooser Title"));
Run Code Online (Sandbox Code Playgroud)
3.支持库ShareCompat:
Activity activity = /* Your activity here */
ShareCompat.IntentBuilder.from(activity)
.setType("message/rfc822")
.addEmailTo(email)
.setSubject(subject)
.setText(body)
//.setHtmlText(body) //If you are using HTML in your body text
.setChooserTitle(chooserTitle)
.startChooser();
Run Code Online (Sandbox Code Playgroud)
Pad*_*mar 200
当你改变你的intent.setType时,你会得到
intent.setType("text/plain");
Run Code Online (Sandbox Code Playgroud)
用于android.content.Intent.ACTION_SENDTO仅获取电子邮件客户端列表,没有Facebook或其他应用程序.只是电子邮件客户端.例如:
new Intent(Intent.ACTION_SENDTO);
Run Code Online (Sandbox Code Playgroud)
我不建议你直接进入电子邮件应用程序.让用户选择他最喜欢的电子邮件应用.不要约束他.
如果您使用ACTION_SENDTO,则putExtra无法向主题添加主题和文本.使用Uri添加主题和正文.
编辑:
我们可以使用message/rfc822而不是"text/plain"MIME类型.但是,这并不表示"仅提供电子邮件客户端" - 它表示"提供支持消息/ rfc822数据的任何内容".这可能很容易包括一些非电子邮件客户端的应用程序.
message/rfc822 支持MIME类型 .mhtml, .mht, .mime
mar*_*wjj 96
这是从Android官方文档引用的,我在Android 4.4上测试过它,并且运行得很好.有关更多示例,请访问https://developer.android.com/guide/components/intents-common.html#Email
public void composeEmail(String[] addresses, String subject) {
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 (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
cap*_*wag 77
一个迟到的答案,虽然我找到了一个可以帮助他人的解决方案:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:abc@xyz.com"));
startActivity(Intent.createChooser(emailIntent, "Send feedback"));
Run Code Online (Sandbox Code Playgroud)
这是我的输出(仅建议使用Gmail +收件箱):
我从Android Developers网站获得了这个解决方案.
小智 34
尝试:
intent.setType("message/rfc822");
Run Code Online (Sandbox Code Playgroud)
Adi*_*ain 32
这对我有用:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL , new String[] { "me@somewhere.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
startActivity(Intent.createChooser(intent, "Email via..."));
Run Code Online (Sandbox Code Playgroud)
即使用ACTION_SENDTO动作而不是ACTION_SEND动作.我在几台Android 4.4设备上试过它,它限制了选择器弹出窗口只显示电子邮件应用程序(电子邮件,Gmail,Yahoo Mail等),它正确地将电子邮件地址和主题插入到电子邮件中.
Avi*_*han 26
这是根据Android Developer Docs的方式 将这些代码行添加到您的应用中:
Intent intent = new Intent(Intent.ACTION_SEND);//common intent
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
Run Code Online (Sandbox Code Playgroud)
如果要添加正文和主题,请添加此项
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject Here");
intent.putExtra(Intent.EXTRA_TEXT, "E-mail body" );
Run Code Online (Sandbox Code Playgroud)
Add*_*dev 14
如果您只想要电子邮件客户端,则应使用android.content.Intent.EXTRA_EMAIL数组.这是一个例子:
final Intent result = new Intent(android.content.Intent.ACTION_SEND);
result.setType("plain/text");
result.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { recipient });
result.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
result.putExtra(android.content.Intent.EXTRA_TEXT, body);
Run Code Online (Sandbox Code Playgroud)
最后拿出最好的办法
String to = "test@gmail.com";
String subject= "Hi I am subject";
String body="Hi I am test body";
String mailTo = "mailto:" + to +
"?&subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(body);
Intent emailIntent = new Intent(Intent.ACTION_VIEW);
emailIntent.setData(Uri.parse(mailTo));
startActivity(emailIntent);
Run Code Online (Sandbox Code Playgroud)
以下代码对我很有用.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc@gmailcom"});
Intent mailer = Intent.createChooser(intent, null);
startActivity(mailer);
Run Code Online (Sandbox Code Playgroud)
小智 8
public void composeEmail(String[] addresses, String subject) {
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 (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我当时发现它与任何角色一起工作的唯一方法.
doreamon的答案是正确的方法,因为它适用于新版Gmail中的所有角色.
老答案:
这是我的.它似乎适用于所有Android版本,主题和消息正文支持,以及完整的utf-8字符支持:
public static void email(Context context, String to, String subject, String body) {
StringBuilder builder = new StringBuilder("mailto:" + Uri.encode(to));
if (subject != null) {
builder.append("?subject=" + Uri.encode(Uri.encode(subject)));
if (body != null) {
builder.append("&body=" + Uri.encode(Uri.encode(body)));
}
}
String uri = builder.toString();
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
context.startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
这些解决方案都不适合我。这是适用于棒棒糖的最小解决方案。在我的设备上,结果选择器列表中仅显示Gmail和本机电子邮件应用程序。
Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
Uri.parse("mailto:" + Uri.encode(address)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(emailIntent, "Send email via..."));
Run Code Online (Sandbox Code Playgroud)
大多数这些答案仅适用于您不发送附件的简单情况。就我而言,我有时需要发送附件 (ACTION_SEND) 或两个附件 (ACTION_SEND_MULTIPLE)。
所以我从这个线程中采用了最好的方法并将它们结合起来。它正在使用支持库,ShareCompat.IntentBuilder但我只显示与 ACTION_SENDTO 与“mailto:”uri 匹配的应用程序。这样我只得到支持附件的电子邮件应用程序列表:
fun Activity.sendEmail(recipients: List<String>, subject: String, file: Uri, text: String? = null, secondFile: Uri? = null) {
val originalIntent = createEmailShareIntent(recipients, subject, file, text, secondFile)
val emailFilterIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
val originalIntentResults = packageManager.queryIntentActivities(originalIntent, 0)
val emailFilterIntentResults = packageManager.queryIntentActivities(emailFilterIntent, 0)
val targetedIntents = originalIntentResults
.filter { originalResult -> emailFilterIntentResults.any { originalResult.activityInfo.packageName == it.activityInfo.packageName } }
.map {
createEmailShareIntent(recipients, subject, file, text, secondFile).apply { `package` = it.activityInfo.packageName }
}
.toMutableList()
val finalIntent = Intent.createChooser(targetedIntents.removeAt(0), R.string.choose_email_app.toText())
finalIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toTypedArray())
startActivity(finalIntent)
}
private fun Activity.createEmailShareIntent(recipients: List<String>, subject: String, file: Uri, text: String? = null, secondFile: Uri? = null): Intent {
val builder = ShareCompat.IntentBuilder.from(this)
.setType("message/rfc822")
.setEmailTo(recipients.toTypedArray())
.setStream(file)
.setSubject(subject)
if (secondFile != null) {
builder.addStream(secondFile)
}
if (text != null) {
builder.setText(text)
}
return builder.intent
}
Run Code Online (Sandbox Code Playgroud)
在Kotlin 中,如果有人正在寻找
val emailArrray:Array<String> = arrayOf("travelagentsupport@kkk.com")
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:") // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, emailArrray)
intent.putExtra(Intent.EXTRA_SUBJECT, "Inquire about travel agent")
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
以下代码对我有用!!
import android.support.v4.app.ShareCompat;
.
.
.
.
final Intent intent = ShareCompat.IntentBuilder
.from(activity)
.setType("application/txt")
.setSubject(subject)
.setText("Hii")
.setChooserTitle("Select One")
.createChooserIntent()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
这对我来说非常好:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("mailto:" + address));
startActivity(Intent.createChooser(intent, "E-mail"));
Run Code Online (Sandbox Code Playgroud)
适用于所有Android版本:
String[] TO = {"email@server.com"};
Uri uri = Uri.parse("mailto:email@server.com")
.buildUpon()
.appendQueryParameter("subject", "subject")
.appendQueryParameter("body", "body")
.build();
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您想确保您的意图仅由电子邮件应用程序(而不是其他文本消息或社交应用程序)处理,请使用该ACTION_SENDTO操作并包含“mailto:”数据方案。例如:
public void composeEmail(String[] addresses, String subject) {
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 (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
我在https://developer.android.com/guide/components/intents-common.html#Email 中找到了这个
| 归档时间: |
|
| 查看次数: |
361876 次 |
| 最近记录: |