我找到了一种通过Android应用发送电子邮件的算法。当我运行它时,它会打开我可以选择发送的应用列表,但是,我只想使用Gmail应用。
这是代码:
protected void sendEmail(String subject, String text) {
Log.i("Send email", "");
String[] TO = {"email@gmail.com"};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("message/rfc822");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, text);
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email!", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
提前致谢!
答案 0 :(得分:0)
隐式意图声明要执行的一般操作,在您的情况下,每个具有电子邮件客户端意图过滤器的应用程序,可以是Gmail,Outlook或无论您的设备有什么电子邮件客户端。
在这种情况下,您可以尝试以下代码:
Intent sendIntentGmail = new Intent(Intent.ACTION_VIEW);
sendIntentGmail.setType("plain/text");
sendIntentGmail.setData(Uri.parse(TextUtils.join(",", addresses)));
sendIntentGmail.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
sendIntentGmail.putExtra(Intent.EXTRA_EMAIL, addresses);
if (subject != null) sendIntentGmail.putExtra(Intent.EXTRA_SUBJECT, subject);
if (body != null) sendIntentGmail.putExtra(Intent.EXTRA_TEXT, body);
mContext.startActivity(sendIntentGmail);
答案 1 :(得分:-1)
您可以使用以下代码:
Intent mailClient = new Intent(Intent.ACTION_VIEW);
mailClient.setClassName("com.google.android.gm",
"com.google.android.gm.ConversationListActivity");
startActivity(mailClient);