Android Sending Email

Android Sending Email

Android Sending Email, this article is all about this word. You all is aware of what’s email.  We are able to also send our private documents also through emails.  Emails can be sent predicated on a user’s language settings.  Numerical messages   – e-mails that focus on ONLY amounts is normally an awful email and generally, most only numerical domains as well. Email is text messages distributed by digital means in one system user to 1 or even more recipients with a network

android sending email

Prior to starting Android Sending Email Activity, You need to know Email efficiency with intent, Objective is hauling data in one element of another element with-in the application form or beyond your application

In order to send an email from the application, you don’t need to implement an email client right from the start, but you may use a preexisting one like the default Android Email app provided from Google android, Gmail, Prospect, K-9 Email etc. For this function, we have to develop an activity that launches an email client, using an implicit Objective with the right data and action. In such a example, we will send an email from our application by using an Intent object that launches existing email clients.

Pursuing section explains various areas of our Intent subject necessary to send an email.

Intent Object – Action to Android sending Email

You shall use ACTION_SEND action to release an email consumer installed on your Google android phone. Following is easy syntax to generate an intent with ACTION_SEND action

Intent emailIntent = new Intent(Intent.ACTION_SEND);

 

Intent Object – Data/Type – Android Sending Email

To send an email you will need to designate mailto: as URI using setData() method and data type is to text/plain using setType() method the following

emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");	

 

Intent Object – Extra – Android Sending Email

Android os has built-in support to increase, SUBJECT, CC, TEST and so on. fields which is often mounted on the purpose before mailing the objective to a aim for email client. You should use following extra domains in your email –

Extra Data & Description

EXTRA_BCC

A String[] having e-mail addresses that needs to be blind carbon copied.

EXTRA_CC

A String[] having e-mail addresses that needs to be carbon copied.

EXTRA_EMAIL

A String[] retaining e-mail addresses that needs to be delivered to.

EXTRA_HTML_TEXT

A frequent String that is from the Intent, used in combination with ACTION_SEND to provide an alternative solution to EXTRA_Text message as HTML formatted content material.

EXTRA_SUBJECT

A continuous string retaining the desired subject matter type of a message.

EXTRA_TEXT

A frequent CharSequence that is from the Intent, used in combination with ACTION_SEND to provide the literal data to be delivered.

 EXTRA_TITLE

A CharSequence dialog name to provide to an individual when used in combination with a ACTION_CHOOSER.

Here is a good example demonstrating you how to assign extra data to your intent

emailIntent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"Recipient"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
emailIntent.putExtra(Intent.EXTRA_TEXT   , "Message Body");

 

The out-put of above syntax is as below as  shown in image

android-sending-email

Android Sending Email Example

Following android sending email example teaches you in sensible how to use intent object to establish Email customer to send a contact to the given recipients.

To send Email test out this android sending email example, you’ll need real Mobile device prepared with latest Android os OS(Android lollipop), otherwise you might get struggle with emulator which might not exactly work properly. Second you’ll need with an Email client like GMail(By default every android version having Gmail client App.

Steps       Description

1- You use Android studio to make an Android program and name it as Androdevelopment under a deal com.example.androdevelopment. While creating this task, ensure you Goal SDK and Compile With at the latest version of Google android SDK to work with higher degrees of APIs.

2- Modify src/MainActivity.java and add required code to care for sending email.

3- Modify design XML res/design/activity_main.xml add any GUI part if required. I’m adding a straightforward button to start Email Client.

4- Modify res/beliefs/strings.xml to establish required frequent values

5- Modify AndroidManifest.xml as given below

6-Run the application form to launch Google android emulator and confirm the consequence of the changes done in the application form.

Following is the content of the modified main activity file  src/com.example.Androdevelopment/MainActivity.java.


Do You Have Any Question About This Tutorial? Ask Question Now On Andro Development & Get Answer Instantly. No Signup/Login Needed


package com.example.androdevelopment;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      Button startBtn = (Button) findViewById(R.id.sendEmail);
      startBtn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
            sendEmail();
         }
      });
   }
   protected void sendEmail() {
      Log.i("Send email", "");
      String[] TO = {""};
      String[] CC = {""};
      Intent emailIntent = new Intent(Intent.ACTION_SEND);
      
      emailIntent.setData(Uri.parse("mailto:"));
      emailIntent.setType("text/plain");
      emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
      emailIntent.putExtra(Intent.EXTRA_CC, CC);
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
      emailIntent.putExtra(Intent.EXTRA_TEXT, "Your Email");
      
      try {
         startActivity(Intent.createChooser(emailIntent, "Send mail..."));
         finish();
         Log.i("Finished sending email...", "");
      }
      catch (android.content.ActivityNotFoundException ex) {
         Toast.makeText(MainActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
      }
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
}

 

Following will be the content of res/layout/activity_main.xml file −

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   
   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Sending Mail Example"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp" />
      
   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:textColor="#ff87ff09"
      android:textSize="30dp"
      android:layout_above="@+id/imageButton"
      android:layout_alignRight="@+id/imageButton"
      android:layout_alignEnd="@+id/imageButton" />
      
   <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageButton"
      android:src="@drawable/abc"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true" />
      
   <Button 
      android:id="@+id/sendEmail"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/compose_email"/>
    
</LinearLayout>

 

Following will be the content of res/values/strings.xml to define two new constants −

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">Androdevelopment</string>
   <string name="hello_world">Hello world!</string>
   <string name="action_settings">Settings</string>
   <string name="compose_email">Compose Email</string>
</resources>

 

Following is the default content of AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.Androdevelopment"
   android:versionCode="1"
   android:versionName="1.0" >
   
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.androdevelopment.MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

 

Let’s make an effort to run our androdevelopment app. I suppose you have linked your actual Google android Mobile device with your personal computer. To perform the app from Android Studio room, wide open one of your project’s activity and click Run Eclipse Run Icon  from the toolbar. Prior to starting your application, Android os studio installer will screen following window to choose a choice where you want to perform your Android request.Choose your mobile device as a choice and check your mobile device that will screen pursuing display screen

android sending email

Now click Compose Email button to list down all the installed email clients. From your list, you can pick one of email clients to send your email. I’ll use Gmail customer to send my email that may have all the provided defaults domains available as shown below. Here From: will be default email ID you have listed for your Android os device.

android sending email

You can adjust either of the given default domains and lastly use send email button to send your email to the talked about recipients.

 

 


Do You Have Any Question About This Tutorial? Ask Question Now On Andro Development & Get Answer Instantly. No Signup/Login Needed