Jer*_*gan 953
最好(也是最简单)的方法是使用Intent:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient@example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)
否则你将不得不编写自己的客户端.
小智 192
使用.setType("message/rfc822")或选择器将显示支持发送意图的所有(许多)应用程序.
Ran*_*ku' 82
我很久以前就一直在使用它,它似乎很好,没有非电子邮件应用程序出现.发送电子邮件意图的另一种方法:
Intent intent = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of email");
intent.putExtra(Intent.EXTRA_TEXT, "Body of email");
intent.setData(Uri.parse("mailto:default@recipient.com")); // or just "mailto:" for blank
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
tig*_*chi 51
我正在使用当前接受的答案的某些内容,以便发送附带二进制错误日志文件的电子邮件.GMail和K-9发送它很好,它也可以在我的邮件服务器上正常运行.唯一的问题是我选择Thunderbird的邮件客户端,它在打开/保存附加的日志文件时遇到了麻烦.事实上,它根本没有保留文件,没有抱怨.
我看了一下这些邮件的源代码,发现日志文件附件(可以理解)是mime类型message/rfc822.当然,附件不是附加的电子邮件.但是Thunderbird无法优雅地应对这个微小的错误.所以这有点令人失望.
经过一些研究和实验,我想出了以下解决方案:
public Intent createEmailOnlyChooserIntent(Intent source,
CharSequence chooserTitle) {
Stack<Intent> intents = new Stack<Intent>();
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
"info@domain.com", null));
List<ResolveInfo> activities = getPackageManager()
.queryIntentActivities(i, 0);
for(ResolveInfo ri : activities) {
Intent target = new Intent(source);
target.setPackage(ri.activityInfo.packageName);
intents.add(target);
}
if(!intents.isEmpty()) {
Intent chooserIntent = Intent.createChooser(intents.remove(0),
chooserTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
intents.toArray(new Parcelable[intents.size()]));
return chooserIntent;
} else {
return Intent.createChooser(source, chooserTitle);
}
}
Run Code Online (Sandbox Code Playgroud)
它可以使用如下:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("*/*");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(crashLogFile));
i.putExtra(Intent.EXTRA_EMAIL, new String[] {
ANDROID_SUPPORT_EMAIL
});
i.putExtra(Intent.EXTRA_SUBJECT, "Crash report");
i.putExtra(Intent.EXTRA_TEXT, "Some crash report details");
startActivity(createEmailOnlyChooserIntent(i, "Send via email"));
Run Code Online (Sandbox Code Playgroud)
如您所见,createEmailOnlyChooserIntent方法可以很容易地使用正确的意图和正确的mime类型.
然后,它会查看响应ACTION_SENDTO mailto协议意图(仅限电子邮件应用程序)的可用活动列表,并根据该活动列表和具有正确mime类型的原始ACTION_SEND意图构建选择器.
另一个优点是Skype不再被列出(这恰好响应了rfc822 mime类型).
小智 34
为了只是让电子邮件应用解决你的意图,你需要指定ACTION_SENDTO的行动和作为上一篇的数据.
private void sendEmail(){
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:" + "recipient@example.com"));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My email's subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "My email's body");
try {
startActivity(Intent.createChooser(emailIntent, "Send email using..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Activity.this, "No email clients installed.", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
Sam*_*Sam 19
使用.setType("message/rfc822")或ACTION_SEND似乎也匹配不是电子邮件客户端的应用程序的策略,例如Android Beam和蓝牙.
使用ACTION_SENDTO和mailto:URI似乎完美地工作,并且在开发人员文档中是推荐的.但是,如果您在官方模拟器上执行此操作并且没有设置任何电子邮件帐户(或者没有任何邮件客户端),则会出现以下错误:
不支持的行动
目前不支持该操作.
如下所示:

事实证明,模拟器将意图解析为被调用的活动com.android.fallback.Fallback,该活动显示上述消息.显然这是设计的.
如果您希望您的应用程序绕过这个,以便它也可以在官方模拟器上正常工作,您可以在尝试发送电子邮件之前检查它:
private void sendEmail() {
Intent intent = new Intent(Intent.ACTION_SENDTO)
.setData(new Uri.Builder().scheme("mailto").build())
.putExtra(Intent.EXTRA_EMAIL, new String[]{ "John Smith <johnsmith@yourdomain.com>" })
.putExtra(Intent.EXTRA_SUBJECT, "Email subject")
.putExtra(Intent.EXTRA_TEXT, "Email body")
;
ComponentName emailApp = intent.resolveActivity(getPackageManager());
ComponentName unsupportedAction = ComponentName.unflattenFromString("com.android.fallback/.Fallback");
if (emailApp != null && !emailApp.equals(unsupportedAction))
try {
// Needed to customise the chooser dialog title since it might default to "Share with"
// Note that the chooser will still be skipped if only one app is matched
Intent chooser = Intent.createChooser(intent, "Send email with");
startActivity(chooser);
return;
}
catch (ActivityNotFoundException ignored) {
}
Toast
.makeText(this, "Couldn't find an email app and account", Toast.LENGTH_LONG)
.show();
}
Run Code Online (Sandbox Code Playgroud)
在开发人员文档中查找更多信息.
Ped*_*ela 19
我用简单的代码行解决了这个问题,正如android文档所解释的那样.
(https://developer.android.com/guide/components/intents-common.html#Email)
最重要的是旗帜:它是ACTION_SENDTO,而不是ACTION_SEND
另一条重要的路线是
intent.setData(Uri.parse("mailto:")); ***// only email apps should handle this***
Run Code Online (Sandbox Code Playgroud)
顺便说一句,如果你发送一个空的Extra,if()最后将无法正常工作,应用程序将无法启动电子邮件客户端.
根据Android文档.如果您想确保您的意图仅由电子邮件应用程序处理(而不是其他文本消息或社交应用程序),请使用该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)
这是示例工作代码,它在 android 设备中打开邮件应用程序并在撰写邮件中自动填充收件人地址和主题。
protected void sendEmail() {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:feedback@gmail.com"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序中使用以下代码。这准确地显示了电子邮件客户端应用程序,例如 Gmail。
Intent contactIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", getString(R.string.email_to), null));
contactIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject));
startActivity(Intent.createChooser(contactIntent, getString(R.string.email_chooser)));
Run Code Online (Sandbox Code Playgroud)
这将仅显示电子邮件客户端(以及出于某种未知原因的 PayPal)
public void composeEmail() {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"hi@example.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Body");
try {
startActivity(Intent.createChooser(intent, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)