Intro

You are working on an app that has a list of contacts and you want to send an email to one of them for example. The easiest way to do this is to open an email client from your application. You also want to send the data from your application to the email client (like the receiver email, the subject and the content of the email) so that you won’t have to enter the same data again in the email client. We will create a simple application for demonstration, where you will enter this data in your application and then on a button click to open your email client with everything ready for you to just press send. We will be using Butterknife library in the project.

Exercise

Create new android project. To keep it simple our application will only have one activity. In the layout we will add some fields for the required input, like who are we sending to, the subject of the email and the content of the email. The layout will look something like this:

Now in our activity we will have to take the input from the edit text fields and use Intents to start our email client. We will create a sendEmail() method where we will create new Intent with the action ACTION_SENDTO. After that we are using putExtra() methods to put our data into the Intent. We are using the setData() method like this i.setData(Uri.parse(“mailto:”)); because this will start only email clients on our phone, and not other messaging services. The complete method will look like this:

 

We are using an String array to put the email address in the Intent because like this we have an option to send the email to multiple receivers.

We will set an onClick() method to the button and call this method when the send button is clicked. And that’s it.

This is how it looks like:

You can find the whole exercise project here: https://github.com/AleksandarGulevski/SendEmailFromAppExercise

Happy coding!

Spread the love

1 Comment

Leave a Reply

Your email address will not be published.