1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Assessing the Features of Android Devices with Kotlin and Java

Sending an e-mail in Android, Kotlin, and Java

Sending an e-mail in Android, Kotlin, and Java
1h 19m

In this course, we'll cover how to access the features of Android devices. You'll learn how to send an SMS, send an email, make a call, and convert speech to text on Android.

Intended Audience

This course is intended for anyone who wants to learn how to start building their own apps on Android.


To get the most out of this course, you should have some basic knowledge of the fundamentals of Android.



Well, hello to everyone out there. In this video, well, we're going to learn how to send an email. Which is the second subject of accessing the features of an Android device. So, we're going to see how email is sent through the application that we develop. Why don't we just continue right here in Android Studio without a single word more? Okay. So, you can see I've created a new project for this section in Android Studio, and of course before we go into all the details, let's just have a look at where we're going. So, here we got three editTexts. One of them will write the address to which the user will be sending the email, the other will write the subject of the email, and as you know, subject section should be filled in when sending an email. So, the user will write the message and write here in the last one. All right, so we can fill in these sections. All right, any email address  in the email address section. I'll write a subject in the subject line and I'll just write something very useful in the message section.

So, when I press the 'Send Email' button, it shows me alternative mail programs installed on the device. See how there's a Gmail here, and well, if other programs were installed on the device it would show them like Outlook or Yandex. I'll choose Gmail, and as you can see when I select Gmail, it writes the address that I entered into the application in the address section. That's a win. And look at that, it wrote the text that I wrote right here in the subject line. And of course I wrote the message in the application to the message section. So, if I press the same button in Gmail, the message is going to be sent. So, that's it. That's all I have to show you. I want to show you how to develop the project. So, let's get to it.

I want to add three editTexts. First editText will be email editText. The second editText will be plain editText. The last one will be multi-line editText. And here I will select these three editTexts and change some of the properties. Cool, so the width of the components can be 300dP, the height can be 50dp, and the text size can be 20sp. And now I want to add a hint to these editTexts. First editText, the hint will be enter address. Second editText, the hint can be enter subject. And for the third, the hint can be enter your email, all right? Now, also we can increase the width of that third editText a little bit. Let's make it 100dp.

All right, so I want to add a button to the design area. The width of the button can be 200dp. The height of the button can be 60dP. Also, the text size can be 20sp. And I will change the text of the button. I want to write send email. Okay, since the main layout is constraint layout, we will determine the constraints of the component. So, I need to select all of the components, right click, select center horizontally. And I also need to determine the vertical constraints. The top constraint value of the first editText, let's make it 50. Top constraint value of the second editText can be 20. And the top constraint value of the third editText can be 20. And then lastly, the top constraint value of the button, let's make that 50.

All right, so there's a quick design now ready. I want to give IDs to these components. So, the ID of the first editText can be editTextAddress. The ID of the second editText can be editText subject, and the ID of the third editText can be editTextMessage.  And of course the ID of the button can just be buttonSend. All right, so now we're ready to go to Kotlin. But this time I'm not going to use the find view by ID function. "What? There's something else?" Sure enough. I'm going to use the Android View Binding feature. So, for this, what I'll do is open the build.gradle module app file. Now, inside the scope of the Android section, I will create a new feature. It's going to be the buildFeatures. Okay?

So, inside the scope of buildFeatures, I will write viewBinding true. And then lastly, I'm going to click the 'Sync Now' button. All right? So with viewBinding we can access the components that we use in the design area from the activity. And look at that, the sync is now complete. Now we got to define the viewBinding in the main activity. So, here I want to create an object from the activity mainBinding. Do you remember way back when we specified that we would be using the viewBinding feature in the gradle file, a class was automatically created just like that for each activity in the application? Maybe you didn't notice, that's okay. But we'll continue inside the onCreate method and you'll see what I'm talking about. All right, so first I want to write mainBinding = ActivityMainBinding.inflate. Inside parenthesis, I'll write layoutInflater.

The next line, I'm going to create a new variable, val view = mainBinding.root. And then lastly, instead of that R.layout.activity_main, I'm going to write the view variable, right? Now we can access components directly in the design area just by using the mainBinding object. So, I need to add a click listener to the button. Write mainBinding.buttonSend.setOnClickListener(), and then here I'll get the data in the editText and assign them into that new variable. So, for that, I'll write val userAddress = mainBinding.editTextAddress.text.toString. For the subject, val userSubject = mainBinding.editTextSubject. text.toString. And lastly, val userMessage = mainBinding.editText.text.toString. All right. So, we get the data entered by the user.

Now we can write the necessary code to send the email. So, I'll write a new function for this. Name of the method, let's just call it sendEmail. So, this method will have three parameters. So, first parameter will be userAddress. Second parameter will be userSubject. Third parameter, userMessage. All right, so I'm going to put the email addresses into an array and I'm going to create that here because this method that I'm going to use to send the email will keep the email addresses in the array. Why is that? Well, that way we only send the email to an address in this project, right? But in real situations, IRL, we may need to send it to many addresses, you know how that works. So, that way, when we transfer all of the email addresses to an array and then assign the array to the sending section, all of the elements in the array, that is, all of those email addresses will be written in the email address section to be sent. Okay, so for this reason I'm creating an array, and the name of the array can be emailAddresses. So, all right. Val emailAddresses = arrayOf{userAddress}. I'll assign the user address to the array as an element.

Okay, so currently the array has one element but of course if I want to I can add as many elements as I want to here. All right, so now we get to the section of sending the email. So, I will send the email with the intent method. Of course, you remember we use the intent method to switch from one activity to another so that we could send data between the activities. Well, it turns out we can also use the intent method to switch to another application and send data to that application. So, here we're going to use intent to send this email. So, in here after I write val emailIntent = Intent, I'll write Intent.ActionSendTo in parenthesis. 

Now there are three different intents used here. First is ACTION_SENDTO. Second one is ACTION_SEND. Third one is ACTION_SENDMULTIPLE. All right, check the difference. So, we usually use the ACTION_SEND  and ACTION_SENDTO, but here's the key. If your goal is to use mail applications, you should use the ACTION_SENDTO. But if you just want to use all communication applications on the device, you should then use the ACTION_SEND. And it turns out I'm going to show you the use of both.

All right, so first I want to use ACTION_SENDTO. So, after writing emailIntent.data, I'll write Uri.parse( uriString:''mailto:'') okay? So, the ACTION_SENDTO includes the mailto data scheme. That way, only the email applications installed on the device will be considered, right? Other applications will just be ignored. And this is a standard and I would say mandatory line of code. All right. But now I have got to add the data to be sent to the mail application using the emailIntent. So, I write emailIntent.putExtra() right in here.

Now if you remember all the way back in that subject called Intent we were sending data with a putExtra method. So, that means first if you remember, I need to send the email address. So, I'm going to write Intent.EXTRA_EMAIL for this. And with this parameter, data will be automatically written to the email address section. So, the data that I want to be written is the name of the array, right? So, the email addresses are inside this array. So, I'll write emailAddresses and in this way all of the addresses in the array will be written in the mail address section. I should also send the mail subject. So, I'm sending the subject with the EXTRA_SUBJECT parameter and the data will be sent is userSubject.

Okay, so finally I should send the message text obviously and I will send it with the EXTRA_TEXT parameter. Okay, and then of course the data that will be sent is userMessage. So, we wrote all of this because the sections that we want to send by mail are these three, right? If there's anything else that you want to send, of course, you can add them in here as well. For example, you've got things like CC, you could select EXTRA_CC as a parameter. Of course, you could also do a BCC but this time you should also take the CC from the user in fact. But I'm just going to delete this part because now I don't want it. It just cluttered anyway. We have to start the Intent but we're not going to start the Intent directly because there might not be any mail application installed on the device. See, we cannot assume therefore, we will check to see by using an if statement. So, if (emailIntent.resolveActivity( packageManager) != null).

So, after performing this check we can start the Intent. After writing the startActivity, I'm writing the Intent.createChooser method. So, this method also takes two parameters. First will be emailIntent. The second will be title, all right? So, here Intent.createChooser method will allow the user to choose alternatives to send the mail. That way the user can send email via the email provider that they want, okay? So, the title is going to be the top description in this selection section.

So, you've got to write what you want to appear here in the title section. Of course, you could also leave this section empty, it's up to you. But by looking at it right, we've completed all the necessary procedures so we just have to make a definition in the manifest file. So, I'll open up the manifest file and between the activity tags in here, we should create a new Intent filter, all right? I'm copying the intent filter section here and just pasting it in. Now we're going to make some changes. So, first of all, I write SENDTO instead of main. Instead of launcher, I'll write, DEFAULT. Finally, we'll add the mailto data scheme, so I'm writing data android:scheme here. To the right of the equals, I will write mailto as a string expression. See, so that way we complete all the necessary procedure to send an email, right? Yeah, so, finally we can, well, just call the sendEmail function inside the on click method. So, I'll write sendEmail. Oh yeah, it takes three parameters. What are they? User address, user subject, user message. Now we can run the application.

So, it opens, I'll write in an email address. I'm going to write a meeting in the subject and for the message section, I'll write, okay, the meeting will start at 9:00. So, when I press the send email button, it shows alternatives. Since there's only the Gmail as the email and the emulator, it's only going to show the Gmail app. So, I'm going to choose an app in the title. I'll choose Gmail and what do you see, write in the address field; android@gmail.com, and it writes meeting in the subject and it writes the message in the message section. So, that's all aligned. What happens when I press the send button? Yeah, it sends the email. All right, so now do you remember, I talked about it earlier that ACTION_SEND method? Let's do it now.

When you want to send an email using the ACTION_SEND method. Do you remember what I said? I'm trying to remember. No, I'm kidding. All communication applications on the device were offered up as an alternative. So, if we make a few changes in the code that we've written, first of all, this will be ACTION_SEND, there's actually going to be no need for data when using the ACTION_SEND method. So, instead we'll just specify the type of mail. So, I am converting this line to the comment line and just below it I'll write emailIntent.type. Now there can be two different types in here. The first is text/plain. Second one is */*. Now we usually use this. It's valid for all email types. All other encodings will be the same, and then finally we can make some changes to the manifest file.

So, this here will be sent. Remember not send to and in addition, we can also use the mime type instead of the mailto data scheme. So, the mime type will be */*, right? Okay, so that's it. We can run the application once again and watch it. Application opens, I'm right in an email address. Then I'll write meeting and meeting will start at 9:00. So, when I press the send email button, look at that. It shows some alternatives. So, this time not only Gmail but all the other messaging applications are shown as alternatives. I'm just going to choose Gmail again. All right, so that's the application on working without any problems. What do you think? You learned how to send mail via the application, all right? That's pretty cool but not cool enough, right? So, we got to continue. I'll see you in the next video.


About the Author
Learning Paths

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics