How to send email using android code | using this tutorial we can easily learn android programming to send email using android application. You need to write few lines of code only as given in the below example.
How to send email using android code Example
Android code given below is the example of sending an email with existing email clients using Intent in android application.
Create a new android application using android studio and give names as howtosendemailinandroidcode.
In case if you are not aware of creating a new project in android studio check this article Steps to install Android studio in Ubuntu or command to Install Android Studio in Ubuntu.
You can also refer the tutorial on how to send SMS using android code to get an idea of android programming.
How can I send emails from my Android application?
In Android programming, you can use ACTION_SEND to call an existing email client in your phone to send an Email.
See following code snippets :
String[] TO={""}; String[] CC={""}; Intent emailintent=new Intent(Intent.ACTION_SEND); emailintent.setData(Uri.parse("mail to:")); emailintent.setType("text/plain"); emailintent.putExtra(Intent.EXTRA_EMAIL,TO); emailintent.putExtra(Intent.EXTRA_CC,CC); emailintent.putExtra(Intent.EXTRA_SUBJECT,"Type Your Subject here"); emailintent.putExtra(Intent.EXTRA_TEXT,"You can write your email message here");
1. Android Layout
The layout of how to send email using android code is as follows.
File : res/layout/activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Click on Button to Send Email" android:id="@+id/txt1" android:textSize="50dp" android:textColor="@color/colorPrimaryDark"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="CLICK HERE TO SEND EMAIL" android:id="@+id/btn2" android:textColor="@color/colorPrimary" android:textSize="20dp" android:onClick="sendEmail"/> </LinearLayout>
2. Activity
Full activity class to send an Email. Read the onClick() method, it should be self-explanatory.
MainActivity.java
package com.example.howtosendemailinandroidcode; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.telephony.SmsManager; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String sPermission= Manifest.permission.INTERNET; try{ if (ActivityCompat.checkSelfPermission(this,sPermission)!= PackageManager.PERMISSION_GRANTED){ //ActivityCompat.requestPermissions(this,new String[]{cPermission},Request_Code_permission); } } catch(Exception e){ e.printStackTrace(); } } public void sendEmail(View v){ Log.i("send email",""); String[] TO={""}; String[] CC={""}; Intent emailintent=new Intent(Intent.ACTION_SEND); emailintent.setData(Uri.parse("mail to:")); emailintent.setType("text/plain"); emailintent.putExtra(Intent.EXTRA_EMAIL,TO); emailintent.putExtra(Intent.EXTRA_CC,CC); emailintent.putExtra(Intent.EXTRA_SUBJECT,"Type Your Subject here"); emailintent.putExtra(Intent.EXTRA_TEXT,"You can write your email message here"); try{ startActivity(Intent.createChooser(emailintent,"Choose an Email client in your phone")); finish(); Log.i("finished sending email",""); } catch (android.content.ActivityNotFoundException ex){ Toast.makeText(this,"there is no email client",Toast.LENGTH_LONG).show(); } } }
3. Demo
In android instead of building an email client from scratch, we can easily send an email from our android application using existing email clients such as GMAIL, Outlook, etc..
The Intent object in android with proper action (Intent.ACTION_SEND) and data will help us to launch the available email clients in our phone to send an email in our application.
Now we will see how to send an email in android application using Intent object with examples.
See default screen and click on the “CLICK HERE TO SEND EMAIL” button.
Output of How to Send Email using android code Example
When we run our project in android studio we will get the result like as shown below.
When you tap on the button it will prompts your existing Email client to select. . In this case, I have selected Gmail, and all previous filled in detail will be populated to Gmail client automatically as shown in the figure.