HTML <a> tag provides you option to specify an email address to send an email. While using <a> tag as an email tag, you will use mailto: email address along with href attribute. Following is the syntax of using mailto instead of using http. This code will generate the following link which you can use to send email.
You can open email client on emulator by configuring your email address with email inbuild with email. Then when call the intent will open and send mail.
Goes like this:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
Alternatively, you could use IntentFactory.getSendEmailIntent(String mailTo, String mailCC, String subject, CharSequence body, File attachment).
To show only email clients use this code:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:[email protected]?subject=" + Uri.encode(subject) + "&body=" + Uri.encode(body));
intent.setData(data);
startActivity(intent);
If you already chose default email client then it will launch it. Otherwise it will show a list of available email clients.
If you have a e-mail address on screen, you can just use in your xml, like this:
android:autoLink="email"
Ok - now the above answer no longer works for me in year 2020. I found something mentioned on google official developer sites which worked for me.
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With