In android, we can easily send an email from our android application using existing email clients such as GMAIL, Outlook, etc. instead of building an email client from scratch.
Generally, the Intent object in android with proper action (ACTION_SEND) and data will help us to launch the available email clients to send an email in our application.
In android, Intent is a messaging object which is used to request an action from another app component such as activities, services, broadcast receivers and content providers. To know more about Intent object in android check this Android Intents with Examples.
To send an email using Intent object in android application, we need to write the code like as shown below.
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, new String[]{"support@tutlane.com"});
it.putExtra(Intent.EXTRA_SUBJECT, "Welcome to Tutlane");
it.putExtra(Intent.EXTRA_TEXT, "Hi Guest, Welcome to Tutlane Tutorial Site");
it.setType("message/rfc822");
it.putExtra(Intent.EXTRA_EMAIL, new String[]{"support@tutlane.com"});
it.putExtra(Intent.EXTRA_SUBJECT, "Welcome to Tutlane");
it.putExtra(Intent.EXTRA_TEXT, "Hi Guest, Welcome to Tutlane Tutorial Site");
it.setType("message/rfc822");
If you observe above code we used multiple components to send email, those are
it - Our local implicit intent
ACTION_SEND - It’s an activity action which specifies that we are sending some data.
putExtra - we use this putExtra() method to add extra information to our Intent. Here we can add following things.
- EXTRA_EMAIL - It’s an array of email addresses
- EXTRA_SUBJECT - The subject of the email that we want to send
- EXTRA_TEXT - The body of the email
The android Intent object is having different options such as EXTRA_CC, EXTRA_BCC, EXTRA_HTML_TEXT, EXTRA_STREAM, etc. to add different options for email client.
setType - We use this property to set the MIME type of data that we want to send. Here we used “message/rfc822” and other MIME types are “text/plain” and “image/jpg”.
Now we will see how to send an email in android application using Intent object with examples.
Android Send Email Example
Following 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 SendMailExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical" >
<EditText
android:id="@+id/txtTo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To"/>
<EditText
android:id="@+id/txtSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>
<EditText
android:id="@+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="Message"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Send"
android:id="@+id/btnSend"/>
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical" >
<EditText
android:id="@+id/txtTo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To"/>
<EditText
android:id="@+id/txtSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>
<EditText
android:id="@+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="Message"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Send"
android:id="@+id/btnSend"/>
</LinearLayout>
Now open our main activity file MainActivity.java from \src\main\java\com.tutlane.sendmailexample path and write the code like as shown below
MainActivity.java
package com.tutlane.sendmailexample;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText eTo;
private EditText eSubject;
private EditText eMsg;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eTo = (EditText)findViewById(R.id.txtTo);
eSubject = (EditText)findViewById(R.id.txtSub);
eMsg = (EditText)findViewById(R.id.txtMsg);
btn = (Button)findViewById(R.id.btnSend);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, new String[]{eTo.getText().toString()});
it.putExtra(Intent.EXTRA_SUBJECT,eSubject.getText().toString());
it.putExtra(Intent.EXTRA_TEXT,eMsg.getText());
it.setType("message/rfc822");
startActivity(Intent.createChooser(it,"Choose Mail App"));
}
});
}
}
public class MainActivity extends AppCompatActivity {
private EditText eTo;
private EditText eSubject;
private EditText eMsg;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eTo = (EditText)findViewById(R.id.txtTo);
eSubject = (EditText)findViewById(R.id.txtSub);
eMsg = (EditText)findViewById(R.id.txtMsg);
btn = (Button)findViewById(R.id.btnSend);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, new String[]{eTo.getText().toString()});
it.putExtra(Intent.EXTRA_SUBJECT,eSubject.getText().toString());
it.putExtra(Intent.EXTRA_TEXT,eMsg.getText());
it.setType("message/rfc822");
startActivity(Intent.createChooser(it,"Choose Mail App"));
}
});
}
}
If you observe above code we used multiple components to send email, those are
it - Our local implicit intent
ACTION_SEND - It’s an activity action which specifies that we are sending some data.
putExtra - we use this putExtra() method to add extra information to our Intent. Here we can add following things.
- EXTRA_EMAIL - It’s an array of email addresses
- EXTRA_SUBJECT - The subject of the email that we want to send
- EXTRA_TEXT - The body of the email
setType - We use this property to set the MIME type of data that we want to send. Here we used “message/rfc822” and other MIME types are “text/plain” and “image/jpg”.
We need to add MIME type in our android manifest file for that open android manifest file (AndroidManifest.xml) and write the code like as shown below
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tutlane.sendmailexample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="message/rfc822"/>
</intent-filter>
</activity>
</application>
</manifest>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="message/rfc822"/>
</intent-filter>
</activity>
</application>
</manifest>
If you observe above AndroidManifest.xml file we added following extra fields of Intent filters.
action - we use this property to define that the activity can perform SEND action.
category - we included the DEFAULT category for this activity to be able to receive implicit intents.
data - the type of data the activity can send.
Output of Android Send Email Example
When we run above program in android studio we will get the result like as shown below.
Once we enter all the details and click on Send button, it will display a dialog with the apps which are capable of sending an email. By selecting the required email client we can send an email without building our own email client like as shown below.
This is how we can send an emails using intents in android applications based on our requirements.
No comments:
Post a Comment