傳送電子郵件
// 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。