We do not need to develop our email client from scratch because they are already available like Gmail and K9mail. But we will need to send email from our android application, where we will have to write an Activity that need to launch an email client and sends an email using our android device. For this purpose, our Acivity will send an ACTION_SEND along with appropriate data load, to the Android Intent Resolver. The specified chooser gives the proper interface for the user to pick how to send our email data.
STEP 1: INTENT OBJECT- Action to send Email
We will use ACTION_SEND action to launch an email client intalled on our Android device.
Following is simple syntax to create an intent with ACTION_SEND action.
Intent emailIntent = new Intent(Intent.ACTION_SEND);
STEP 2: INTENT OBJECT- Data/Type to send Email
To send an email we need to specify mailto: as URI using setData() method and datatype will be tottext/plain using setType() method as follows:
emailItent.setData(Uri.parse("mailto:")); emailIntent.settype.settype("text/plain");
STEP 3: INTENT OBJECT- Extra to send Email
Android has built-in support to add TO,SUBJECT,CC,TEXT etc. fields which can be attached to intent before sending the intent to target email client.
Here is an example showing how to assign extra data to your intent:
emailIntent. putExtra(Intent.EXTRA_EMAIL,new String[]{"recipent@example.com"}); emailIntent.putExtra(Intent.EXTRA_SUBJECT,"subject of email"); emailIntent.putExtra(Intent.EXTRA_TEXT,"body of email");