How to send a mail explicit intent
To send an email using an explicit intent in Android, you can use the Intent
class and specify the ACTION_SENDTO
action. Here's an example:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:[email protected]")); // recipient's email address
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of the email");
intent.putExtra(Intent.EXTRA_TEXT, "Body of the email");
try {
startActivity(Intent.createChooser(intent, "Send email"));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
In this example:
Intent.ACTION_SENDTO
specifies the action to send an email.Uri.parse("mailto:[email protected]")
specifies the recipient's email address.putExtra(Intent.EXTRA_SUBJECT, "Subject of the email")
sets the subject of the email.putExtra(Intent.EXTRA_TEXT, "Body of the email")
sets the body of the email.startActivity(Intent.createChooser(intent, "Send email"))
starts the email client chooser activity, allowing the user to select an email client to send the email.
Note that this code will only work if an email client is installed on the device. If no email client is installed, the ActivityNotFoundException
will be thrown.
You can also add additional extras to the intent to specify other email settings, such as:
putExtra(Intent.EXTRA_CC, "[email protected]")
to specify CC recipientsputExtra(Intent.EXTRA_BCC, "[email protected]")
to specify BCC recipientsputExtra(Intent.EXTRA_ATTACHMENT, "/path/to/file")
to specify an attachment
For example:
intent.putExtra(Intent.EXTRA_CC, "[email protected]");
intent.putExtra(Intent.EXTRA_BCC, "[email protected]");
intent.putExtra(Intent.EXTRA_ATTACHMENT, "/path/to/file");
Keep in mind that the availability of these extras may vary depending on the email client being used.