Normally when you send email using android you rely on preinstalled apps like Gmail etc. But what if you want to send an email from inside your app without user interaction?? Read on to find the easiest method to do so...
/res/layout/activity_main.xml
Sending email should always be done in a AsyncTask so that it may run in background without consuming your apps time and rendering it slow.
The following code shows the LongOperation AsyncTask which sends your mail:
/src/yourpackage/LongOperation.java
For the GMailSender class you need to include these two java files in your code, just copy the two files and change the package name to your package name and use them: GMailSender.java
JSSEProvider.java
The sending() function which is called when user clicks the button in the layout is defined as follows:
/src/yourpackage/MainActivity.java
Your AndroidManifest.xml file must contain the permission to use internet:
/AndroidManifest.xml
Do check that your device is connected to INTERNET before running the code.
Layout Code
The layout of this app contains a Button which onclicking it sends the email:
/res/layout/activity_main.xml
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="sending"/>
</RelativeLayout>
Java Code
Sending email should always be done in a AsyncTask so that it may run in background without consuming your apps time and rendering it slow.
The following code shows the LongOperation AsyncTask which sends your mail:/src/yourpackage/LongOperation.java
public class LongOperation extends
AsyncTask<Void, Void, String>
{
@Override
protected String
doInBackground(Void... params)
{
try{GMailSender sender = new GMailSender("sender.sendermail.com","senders password");
sender.sendMail("subject",
"body",
"sender.sendermail.com",
"reciepients.recepientmail.com");
}
catch(Exception e)
{
Log.e("error",e.getMessage(),e);
return "Email
Not Sent";
}
return "Email
Sent";
}
@Override
protected void
onPostExecute(String result)
{
}
@Override
protected void onPreExecute()
{
}
@Override
protected void
onProgressUpdate(Void... values)
{
}
}
For the GMailSender class you need to include these two java files in your code, just copy the two files and change the package name to your package name and use them: GMailSender.java
JSSEProvider.java
The sending() function which is called when user clicks the button in the layout is defined as follows:
/src/yourpackage/MainActivity.java
public void sending(View
v)
{
try
{
LongOperation l=new
LongOperation();
l.execute(); //sends the email in background
Toast.makeText(this, l.get(),
Toast.LENGTH_SHORT).show();
} catch (Exception e)
{
Log.e("SendMail",
e.getMessage(), e);
}
}
Your AndroidManifest.xml file must contain the permission to use internet:
/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
Do check that your device is connected to INTERNET before running the code.