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.
After Download above RAR, Extract it.
Here is the full description of project below, I will go through step by step.
Before we begin you need to download few library files and 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.
Thank you!!
ReplyDeleteThis app really works.....
It doesn't work
ReplyDeletehow do u call this automatic when u have to enter the mail ids and also enter the credentials
ReplyDeleteUsually, 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.
DeletePandya, Could you please let me know how to configure my own server and email services in my app...
DeleteIt doesn't work
ReplyDeleteCheck, 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.
Deletethat is work great! #thankYou
ReplyDeletenew Thread(new Runnable() {
ReplyDeletepublic void run() {
--------------------------just put this line inside a thread ->
sender.sendMail(sub,body,emailId,to);
}
}).start();
thanks it works...
ReplyDeletebut 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
This comment has been removed by the author.
ReplyDeleteit shows message sent,,,but message not received,,,help me friends...
ReplyDeleteit shows message sent,,,but message not received,,,help me friends...
ReplyDeleteThis comment has been removed by the author.
DeleteNot working app stopped...:/
ReplyDeleteNot working app stopped
ReplyDeleteShows Class not Found Error
ReplyDeleteits not working
ReplyDeleteThis comment has been removed by the author.
ReplyDeletei need a link to source code
ReplyDeletei need source code
ReplyDeleteSOURCE CODE LINKS ARE NOT WORK SO PLEASE PROVIDE CODE
ReplyDeletefake
ReplyDeletewhat is GmailSender
ReplyDeleteRespect and I have a swell offer you: How To Become A House Renovation split level house kitchen remodel
ReplyDelete