Saturday 26 April 2014


email using intent
Smartphones allows to access to lots of different activities as well as sending text and calling some one. These include the ability to surf the web from your handset and even sync your device with an email account to read and send messages from your mobile phone. Smartphones running Google Android are no exception. You can easily configure your smartphone to access your primary email account with commonly built in Gmail app in all the smartphones.
     So today I am going to show you how to send email from your application using intent to send it using gmail. 

Some Advantages of Email:

  • Emails are easy to use. You can organize your daily correspondence, send and receive electronic messages.
  • Emails are fast. They are delivered at once around the world. No other form of written communication is as fast as an email.
  • The language used in emails is simple and informal.
  • When you reply to an email you can attach the original message so that when you answer the recipient knows what you are talking about. This is important if you get hundreds of emails a day.
  • It is possible to send automated emails with a certain text. In such a way it is possible to tell the sender that you are on vacation. These emails are called auto responders.
  • Emails do not use paper. They are environment friendly and save a lot of trees from being cut down.
  • Emails can also have pictures in them. You can send birthday cards or newsletters as emails.
  • Products can be advertised with emails. Companies can reach a lot of people and inform them in a short time.

OutPut:

emailOnlaunchemail Text and Image

email attach imageemail selectemail send using gmail
Create new Android Project
Project Name: SendEmail
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: SendEmail
Package Name: com.shaikhhamadali.blogspot.sendemail
Create Layout file: activity_send_email


1. code of Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- set input type email to launch soft keyboard containing .com and @ sign -->

    <EditText
        android:id="@+id/edTMail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="email address"
        android:inputType="textEmailAddress" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/edTSubject"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="subject" />

    <EditText
        android:id="@+id/edTMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="message" />

    <EditText
        android:id="@+id/edTSignature"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="signature" />

    <ImageButton
        android:id="@+id/imBtnImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxHeight="150dp"
        android:maxWidth="150dp"
        android:src="@drawable/attachment" />

    <Button
        android:id="@+id/btnSendMail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="send email" />

</LinearLayout>

2. code of activity:


package com.shaikhhamadali.blogspot.sendemail;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class SendEmail extends Activity {
 //Create variables
 Uri selectedImageUri;
 //declare views/controls
 Button btnSendMail;
 ImageButton imBtnImage;
 EditText edTMail,edTSubject,edTMessage,edTSignature;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_send_email);
  initializeControls();
 }
 private void initializeControls() {
  //get reference of the UI Controls
  edTMail=(EditText)findViewById(R.id.edTMail);
  edTSubject=(EditText)findViewById(R.id.edTSubject);
  edTMessage=(EditText)findViewById(R.id.edTMessage);
  edTSignature=(EditText)findViewById(R.id.edTSignature);
  imBtnImage=(ImageButton)findViewById(R.id.imBtnImage);
  btnSendMail =(Button)findViewById(R.id.btnSendMail);
  //Set click listeners to Buttons
  imBtnImage.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // Create instance of intent
    Intent intent = new Intent();
    // set type as image
    intent.setType("image/*");
    /*set action as ACTION_GET_CONTENT(Activity Action:
                Allow the user to select a particular kind of data and return it.)*/
    intent.setAction(Intent.ACTION_GET_CONTENT);
    /*start activity for result and pass create Chooser 
                 and set title of dialog as Select Picture*/
    startActivityForResult(Intent.createChooser(intent,
      "Select Picture"), 1);
   }
  });
  btnSendMail.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

    // Create instance of intent
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{edTMail.getText().toString()});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, edTSubject.getText().toString());
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, edTMessage.getText().toString()+"\n\n"+edTSignature.getText().toString()); 
    // check that is user picked an image or not
    if(selectedImageUri!=null){
     emailIntent.setType("image/jpeg");
     //attach image to email by telling intent the uri of image
     emailIntent.putExtra(Intent.EXTRA_STREAM, selectedImageUri);
    }
    else emailIntent.setType("text/plain");
    /*start activity and pass create Chooser 
                  and set title of dialog as Send your email in: */
    startActivity(Intent.createChooser(emailIntent, "Send your email in:"));
   }
  });
 }
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  //RESULT_OK means that user has selected an image 
  if (resultCode == RESULT_OK) {
   //check that is the same request code that pass above 
   if (requestCode == 1) {
    //get uri from data for attachment as image
    selectedImageUri = data.getData();
    //set image on imagebutton to show the selection
    imBtnImage.setImageURI(selectedImageUri);
   }
  }
 }

}

3. note that:

4. conclusion:

  • Some information about how to email using intent.
  • Some information about how to pick image using intent.
  • know how to attach email and add signature using intent.
  • Know how ACTION_SEND help to email text and attach image as well.
  • know what are some of the advantages of using email.

    5. About the post:

    • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
    •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
    •  Hope you enjoy it!
        6. Source Code:
                you can download the source code from: GoogleDriveGithub

        Cheers,
        Hamad Ali Shaikh