单击按钮时从 Android 应用程序发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21720640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending Email from Android app when click on button
提问by Amardeepvijay
I need to provide feature for users where users can share some data by sending email. I used below code.
我需要为用户提供功能,用户可以通过发送电子邮件来共享一些数据。我使用了下面的代码。
Intent email = new Intent(android.content.Intent.ACTION_SENDTO);
email.setType("message/rfc822");
email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(email,"Choose an Email client :"));
This shows Email, gmail, Skype and send via bluetooth for user to choose. I dont want user to show Skype,send via bluetooth in this list. What i need to do ? I have WhatsApp in my phone, which does same thing, but doesn't show Email, bluetooth in the list(Settings->help->Contactus->...).only show Email and Gmail in list. I need to do the same.
这显示电子邮件、gmail、Skype 和通过蓝牙发送供用户选择。我不希望用户在此列表中显示 Skype,通过蓝牙发送。我需要做什么 ?我的手机中有 WhatsApp,它做同样的事情,但不在列表中显示电子邮件、蓝牙(设置->帮助->联系人->...)。只在列表中显示电子邮件和 Gmail。我需要做同样的事情。
回答by localhost
Try this:
尝试这个:
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","[email protected]", null));
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(intent, "Choose an Email client :"));
If you don't have a specific recipient - go like this:
如果您没有特定的收件人 - 像这样:
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", "", null));
回答by Maulik.J
use this method to share via gmail only jus you need to call
使用此方法通过 gmail 共享,只需您需要调用
startActivity(getSendEmailIntent(context, email,subject, body));
public Intent getSendEmailIntent(Context context, String email,
String subject, String body) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
try {
// Explicitly only use Gmail to send
emailIntent.setClassName("com.google.android.gm",
"com.google.android.gm.ComposeActivityGmail");
emailIntent.setType("text/html");
// Add the recipients
if (email != null)
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { email });
if (subject != null)
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
subject);
if (body != null)
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body));
// Add the attachment by specifying a reference to our custom
// ContentProvider
// and the specific file of interest
// emailIntent.putExtra(
// Intent.EXTRA_STREAM,
// Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"
// + fileName));
return emailIntent;
// myContext.startActivity(emailIntent);
} catch (Exception e) {
emailIntent.setType("text/html");
// Add the recipients
if (email != null)
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { email });
if (subject != null)
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
subject);
if (body != null)
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body));
// myContext.startActivity(Intent.createChooser(emailIntent,
// "Share Via"));
return emailIntent;
}
}