Programmatically Send an Email from Android App using Intent

Android Send Email from Activity
Android Send Email from Activity

Intents are messenger objects that are used to call Activities from a particular activity class. There are two types of Intents that you must know Explicit and Implicit Intents.

Implicit intents are used to call Activities that are defined within your project folder. To call other App Activity we need to use Explicit intents.

The code to send an email through your Android application from a Activity class is pretty simple. When this block of code is executed, a Dialog box is displayed with all the Email clients that are available on the device (e.g. Gmail, Mail, Outlook, Hotmail, Yahoomail etc), If no email client is found that you may get a pop-up saying "No Application can perform this action".

Steps:

  1. Create an Intent object emailIntent with Intent.ACTION_SEND
  2. Set type as message/rfc822 i.e. for MIME type
  3. Use Intent.EXTRA_EMAIL as a key to putExtra and where you need to add the Email-ID of the recipient.
  4. Use Intent.EXTRA_SUBJECT key and pass the value as the Subject of Email.
  5. Similarly Intent.EXTRA_TEXT value is the Body of the Email.
  6. In a try catch block call startActivity()
  7. In the Catch block if ActivityNotFoundException has occurred, i.e No Email client is found, we display a Toast Message.

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!