通过gmail发送电子邮件
问题描述:
我用编程的方式使用 fires intent来实现发送电子邮件的功能:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL,
new String[] { to });
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, msg);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast
.makeText(Start.this,
"There are no email clients installed.",
Toast.LENGTH_SHORT).show();
}
但是当这个intent被销毁后,我看到list中很多的项目,例如sms app , gmail app, facebook app 等等。
如何把这些都过滤掉,只剩下gmail app? 或者只有email apps?
答
使用android.content.Intent.ACTION_SENDTO (new Intent(Intent.ACTION_SENDTO);
只能获得电子邮件客户的列表。
使用Uri来添加主题和正文文本。
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText;
uriText = "mailto:email@gmail.com" +
"?subject=the subject" +
"&body=the body of the message";
uriText = uriText.replace(" ", "%20");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send mail..."));
答
使用
// 只用于提示电子邮件客户端
i.setType("message/rfc822");
代替
i.setType("text/plain");