我正在开发一个应用程序在Android。我不知道如何从应用程序发送电子邮件?
当前回答
为了发送带有附加二进制错误日志文件的电子邮件,我使用了当前接受的答案。GMail和K-9发送得很好,在我的邮件服务器上也很好。唯一的问题是我选择的邮件客户端Thunderbird,它在打开/保存附加的日志文件时遇到了麻烦。事实上,它根本没有保存文件,没有抱怨。
我查看了其中一封邮件的源代码,注意到日志文件附件具有mime类型消息/rfc822(这是可以理解的)。当然这个附件不是附件邮件。但雷鸟无法优雅地处理这个微小的错误。所以这有点令人沮丧。
经过一些研究和实验,我想出了以下解决方案:
public Intent createEmailOnlyChooserIntent(Intent source,
CharSequence chooserTitle) {
Stack<Intent> intents = new Stack<Intent>();
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
"info@example.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);
}
}
它可以这样使用:
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"));
如您所见,可以很容易地为createEmailOnlyChooserIntent方法提供正确的意图和正确的mime类型。
然后,它遍历响应ACTION_SENDTO邮件协议意图(仅是电子邮件应用程序)的可用活动列表,并基于该活动列表和具有正确mime类型的原始ACTION_SEND意图构造一个选择器。
另一个优点是Skype不再被列出(这恰好响应rfc822 mime类型)。
其他回答
使用这个发送电子邮件…
boolean success = EmailIntentBuilder.from(activity)
.to("support@example.org")
.cc("developer@example.org")
.subject("Error report")
.body(buildErrorReport())
.start();
使用build gradle:
compile 'de.cketti.mailto:email-intent-builder:1.0.0'
试试这个:
String mailto = "mailto:bob@example.org" +
"?cc=" + "alice@example.com" +
"&subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(bodyText);
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse(mailto));
try {
startActivity(emailIntent);
} catch (ActivityNotFoundException e) {
//TODO: Handle case where no email app is available
}
上面的代码将打开用户最喜欢的电子邮件客户端预填充的电子邮件准备发送。
源
/**
* Will start the chosen Email app
*
* @param context current component context.
* @param emails Emails you would like to send to.
* @param subject The subject that will be used in the Email app.
* @param forceGmail True - if you want to open Gmail app, False otherwise. If the Gmail
* app is not installed on this device a chooser will be shown.
*/
public static void sendEmail(Context context, String[] emails, String subject, boolean forceGmail) {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:"));
i.putExtra(Intent.EXTRA_EMAIL, emails);
i.putExtra(Intent.EXTRA_SUBJECT, subject);
if (forceGmail && isPackageInstalled(context, "com.google.android.gm")) {
i.setPackage("com.google.android.gm");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else {
try {
context.startActivity(Intent.createChooser(i, "Send mail..."));
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No email app is installed on your device...", Toast.LENGTH_SHORT).show();
}
}
}
/**
* Check if the given app is installed on this devuice.
*
* @param context current component context.
* @param packageName The package name you would like to check.
* @return True if this package exist, otherwise False.
*/
public static boolean isPackageInstalled(@NonNull Context context, @NonNull String packageName) {
PackageManager pm = context.getPackageManager();
if (pm != null) {
try {
pm.getPackageInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
return false;
}
为了让电子邮件应用程序来解决你的意图,你需要指定ACTION_SENDTO作为动作和mailto作为数据。
private void sendEmail(){
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:" + "recipient@example.com")); // You can use "mailto:" if you don't know the address beforehand.
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();
}
}
发送电子邮件可以用intent完成,不需要配置。但这样就需要用户交互,布局也会受到一些限制。
在没有用户交互的情况下构建和发送更复杂的电子邮件需要构建自己的客户端。第一件事是用于电子邮件的Sun Java API不可用。我曾经成功地利用Apache Mime4j库来构建电子邮件。都是基于nilvec的文件。
推荐文章
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。
- 如何以编程方式创建ColorStateList ?
- forceLayout(), requestLayout()和invalidate()的用法
- 如何增加Android模拟器的存储?(INSTALL_FAILED_INSUFFICIENT_STORAGE)
- 你怎么知道布局已经画好了?