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


24 comments:

  1. Thank you!!
    This app really works.....

    ReplyDelete
  2. how do u call this automatic when u have to enter the mail ids and also enter the credentials

    ReplyDelete
    Replies
    1. Usually, android app developer uses default app for Email service like, Gmail, Yahoo etc. But If you have your own server and Email service then you can directly send email without open any email app. This is the main advantage of it.

      Delete
    2. Pandya, Could you please let me know how to configure my own server and email services in my app...

      Delete
  3. Replies
    1. Check, whether you have given right credential or not. If you are using own Gmail account then Gmail will send you one permission mail. You need to give permission at access your account from 3rd party library.

      Delete
  4. new Thread(new Runnable() {
    public void run() {
    --------------------------just put this line inside a thread ->
    sender.sendMail(sub,body,emailId,to);
    }
    }).start();

    ReplyDelete
  5. thanks it works...
    but i have a dought that if i write wrong email id in first time asked textbox than also it will successfully send the mail,... so wnat to know that hoe it can be possible??
    please give me ans as soon as possible.. thank you

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. it shows message sent,,,but message not received,,,help me friends...

    ReplyDelete
  8. it shows message sent,,,but message not received,,,help me friends...

    ReplyDelete
  9. Shows Class not Found Error

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. SOURCE CODE LINKS ARE NOT WORK SO PLEASE PROVIDE CODE

    ReplyDelete