Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening an email client on clicking a button

Tags:

android

email

People also ask

How do I open an email client in HTML?

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.

How to open email in android studio?

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);
    }