Android Send Email
In this article, we’ll walk you through the steps to add Android Send Email feature to your app.
As an Android developer, you may need to include the ability for users to send emails from within your app. Fortunately, Android provides a simple and straightforward way to do this using Intent.ACTION_SEND.
You can use the Android platform’s API to send an email using an Intent.
The Intent serves as an object that represents an action to be performed, and it can be utilized to initiate activities or services.
Step 1: Add Email Permission in Manifest File
Before you start with the email sending process, you need to add the necessary permission in the AndroidManifest.xml file. You can add the following line of code to the file:
<uses-permission android:name="android.permission.INTERNET" />
Step 2: Create an Intent with Email Address
To start the email sending process, you need to create an Intent that will launch the email app on your device.
You can use the ACTION_SEND action to create the Intent and specify the email address, subject, and body of the email.
Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("message/rfc822"); intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" }); intent.putExtra(Intent.EXTRA_SUBJECT, "Email Subject"); intent.putExtra(Intent.EXTRA_TEXT, "Email body");
To set the MIME type of the Intent to “message/rfc822” for an email message, you can use the setType() method. This method is used to define the type of data that is being sent or received by the Intent. In the case of “message/rfc822”, it indicates that the message is in the format of an RFC 822 message, which is a standard format for email messages.
Step 3: Launch Email App Intent
To launch the email app on your device, you can use the startActivity() method after creating the Intent for sending an email.
startActivity(Intent.createChooser(intent, "Send Email"));
When you use the createChooser() method, it will show a list of email apps that are installed on your device to choose from.
Full Code example:
public void sendEmail() { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("message/rfc822"); intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" }); intent.putExtra(Intent.EXTRA_SUBJECT, "Email Subject"); intent.putExtra(Intent.EXTRA_TEXT, "Email body"); startActivity(Intent.createChooser(intent, "Send Email")); }
In conclusion, adding email sending functionality to your Android app is a straightforward process that can be done using the Intent.ACTION_SEND API. By following the steps outlined in this article, you can quickly and easily add this feature to your app, giving users the ability to send emails directly from within your app.