java - 我们如何在从应用程序传递电子邮件意图时动态设置来自电子邮件
问题描述
我想通过我的应用程序中的电子邮件意图发送文件。但我想根据我的选择通过代码设置电子邮件 FROM id,而不是默认情况下在我的设备中使用 gmail 登录。Android 代码可以吗?
public void sendEmail() {
try {
Uri photoURI = FileProvider.getUriForFile(UserDetails.this, getApplicationContext().getPackageName() + ".provider", jsonFile);
String email = user.getEmail();
String subject = "User Export Data";
String message = "For getting the better understanding.Please open this json file with the json viewer tools.";
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{email});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
if (jsonFile != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, photoURI);
}
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
this.startActivity(emailIntent);
// this.startActivity(Intent.createChooser(emailIntent, "Sending email..."));
} catch (Throwable t) {
Toast.makeText(this, "Request failed try again: " + t.toString(), Toast.LENGTH_LONG).show();
}
}
解决方案
尝试这个
this.startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:to@gmail.com")));
或者
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String\[\]{ "serveroverloadofficial@gmail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hello There");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Add Message here");
emailIntent.setType("message/rfc822");
try {
startActivity(Intent.createChooser(emailIntent,
"Send email using..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(),
"No email clients installed.",
Toast.LENGTH_SHORT).show();
}
}
});
推荐阅读
- c++ - 解析引用 | Exe/Dll 与 Lib
- php - FPDF 分节符
- hive - 为什么 hive 在 Join 操作期间不选择 SerDe JAR?
- javascript - 如何在节点中的多个文件之间同步缓存对象?
- java - (LibGDX) Android 应用程序在创建 FrameBuffers 时崩溃
- python-2.7 - 如何在视频中找到鼠标悬停位置(最好是时间戳)?
- responsive-design - 如何使静态 html 页面缩小或拉伸以适应移动屏幕?
- sql-server - 对 SQL Server 中的表行应用过滤器
- python-3.x - Python -GTTS-Twilio - 短信mp3
- android - 在 Android Webview 窗口中打开工作只需一次