Showing posts with label send mail without intent. Show all posts
Showing posts with label send mail without intent. Show all posts

Friday, September 6, 2013

Send Mail Automatically without Intent Tutorial

You can use intent to send mail.It uses inbuilt Email application to send mail.You can also send mail without using inbuilt email application.You need to have knowledge of SMTP (Simple Mail Transfer Protocol). There are many libraries available for send mail automatically.I use some of that for this tutorial.

Before we begin you need to download few library files and java class files.


  1. Auto send Mail Libraries
  2. Java Class Files

After Download above RAR, Extract it.

Here is the full description of project below, I will go through step by step.


  • Create android project named "SendEmailAuto".
  • Now Add library files (Above First Link) to your project (Right click your project > Properties > Java Build Path > Libraries > Add External JARs. Now Go to Order and Export Tab > Checked all JARs and press OK button)
  • Add Java class files (Above second link) into project src folder.Your Project Hierarchy is look like:



  • Now add following code in activity_main.xml

activity_main.xml



<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="25dp" >

        <EditText
            android:id="@+id/etemail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter Your Email ID"
            android:inputType="textEmailAddress"
            android:lines="1" />

        <EditText
            android:id="@+id/etPass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter Your Password"
            android:inputType="textPassword"
            android:lines="1" />

        <Button
            android:id="@+id/btnCompose"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Compose" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            android:id="@+id/composeLinear"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/etTo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="To"
                android:lines="1" >

                <requestFocus />
            </EditText>

            <EditText
                android:id="@+id/etSubject"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Subject"
                android:lines="1" />

            <EditText
                android:id="@+id/etBody"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:gravity="left|top"
                android:hint="Enter Text Here"
                android:lines="10" />

            <Button
                android:id="@+id/btnSend"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Send" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>


  • Add following code in MainActivity.java

MainActivity.java


public class MainActivity extends Activity {

Button btnSend,btnCompose;
EditText etemailId,etPass,etTo,etSub,etBody;
String emailId,spass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSend = (Button)findViewById(R.id.btnSend);
btnCompose = (Button)findViewById(R.id.btnCompose);
etemailId = (EditText)findViewById(R.id.etemail);
etPass = (EditText)findViewById(R.id.etPass);
etTo = (EditText)findViewById(R.id.etTo);
etSub = (EditText)findViewById(R.id.etSubject);
etBody = (EditText)findViewById(R.id.etBody);
final LinearLayout layout= (LinearLayout)findViewById(R.id.composeLinear);
btnSend.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String sub = etSub.getText().toString();
String to = etTo.getText().toString();
String body = etBody.getText().toString();
try {   
       GmailSender sender = new GmailSender(emailId, spass);
       sender.sendMail(sub,body,emailId,to);  
       Toast.makeText(getApplicationContext(),"Mail has been sent.",Toast.LENGTH_SHORT).show();
   } catch (Exception e) {   
    Toast.makeText(getApplicationContext(),"Error! Try again later.",Toast.LENGTH_SHORT).show();
   }
}
});
btnCompose.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
emailId = etemailId.getText().toString();
spass = etPass.getText().toString();
if(emailId.equals("") || spass.equals("")){
Toast.makeText(getApplicationContext(),"Email ID and Password fields are mandatory.",Toast.LENGTH_SHORT).show();
}else{
layout.setVisibility(View.VISIBLE);
}
}
});
}
}

  • If you get error,Don't worry. Just press Ctrl + Shift + O button. It will import all missing libraries.
  • Don't forget to give internet permission in AndroidMenifest.xml file.
  • Open AndroidMenifest.xml file and add below permissions, before <application> tag.

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

  • Done ! Now run the project.
NOTE : Don't use Android emulator for testing,It will not work on Android emulator. Use real android device for testing.Before run this project check your emailID and password.  

Download Full Source code : Download