发送电子邮件
// Compile a Uri with the 'mailto' schema
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","johndoe@example.com", null));
// Subject
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello World!");
// Body of email
emailIntent.putExtra(Intent.EXTRA_TEXT, "Hi! I am sending you a test email.");
// File attachment
emailIntent.putExtra(Intent.EXTRA_STREAM, attachedFileUri);
// Check if the device has an email client
if (emailIntent.resolveActivity(getPackageManager()) != null) {
// Prompt the user to select a mail app
startActivity(Intent.createChooser(emailIntent,"Choose your mail application"));
} else {
// Inform the user that no email clients are installed or provide an alternative
}
这将在用户选择的邮件应用程序中预先填写电子邮件。
如果你需要添加附件,可以使用 Intent.ACTION_SEND
而不是 Intent.ACTION_SENDTO
。对于多个附件,你可以使用 ACTION_SEND_MULTIPLE
提醒一句:不是每一个设备具有 ACTION_SENDTO
供应商,并要求 startActivity()
而不检查 resolveActivity()
第一可能抛出 ActivityNotFoundException。