Android set mail intent using url that includes subject
To set a mail intent in Android using a URL that includes the subject, you can use the following approach:
- Create a
Uri
object with the mailto scheme and the URL that includes the subject. - Use the
Intent
constructor to create a newIntent
object with theACTION_SENDTO
action and theUri
object as the data. - Set the
TYPE_EMAIL
type on theIntent
object to indicate that it's an email intent. - Optionally, you can set the
EXTRA_SUBJECT
extra on theIntent
object to specify the subject of the email.
Here's some sample code:
String url = "mailto:[email protected]?subject=Hello%20World!";
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_SUBJECT, "Hello World!");
startActivity(Intent.createChooser(intent, "Send email"));
In this example, the url
variable contains the mailto URL with the subject included. The Uri
object is created using the Uri.parse()
method, and then passed to the Intent
constructor along with the ACTION_SENDTO
action.
The setType()
method is used to set the type of the intent to message/rfc822
, which indicates that it's an email intent.
The putExtra()
method is used to set the EXTRA_SUBJECT
extra on the intent, which specifies the subject of the email.
Finally, the startActivity()
method is used to start the activity chooser, which will allow the user to select an email app to send the email.
Note that the EXTRA_SUBJECT
extra is optional, and you can omit it if you don't want to specify a subject for the email.