Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AsyncTask to Send Android Email

I had recently asked a question regarding the following code:

Sending Email in Android using JavaMail API without using the default/built-in app

I had asked this in regards to a network error, as per a previous question:

Need Help Debugging Email Code

My question is, how would I implement an AsyncTask in order to successfully send an email with this Android code? Every tutorial that I see informs me that I should do

extend AsyncTask {

However, GMailSender.java already has this defined as:

public class GMailSender extends javax.mail.Authenticator

Would anyone be able to help me? Thanks!

NOTE:

Please don't be like the idiot who had -1ed this question and posted the exact answer as was given in Sending Email in Android using JavaMail API without using the default/built-in app. I am unable to use that exact coding, due to the fact that it is no longer possible to run a network operation on the main thread of an Android application. I am looking for a way to use AsyncTask in order to run the operation in the background. What I am unable to find out is how to do

extend AsyncTask {

without touching

public class GMailSender extends javax.mail.Authenticator
like image 463
The Obscure Question Avatar asked Jan 17 '13 08:01

The Obscure Question


People also ask

Can we use AsyncTask in android?

Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive. Android application runs on a single thread when launched.

Is AsyncTask deprecated in android?

AsyncTask is used to perform time talking operations in android, but it's marked as deprecated from android 11.

What is the use of AsyncTask in android?

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

Is it possible to start AsyncTask from background thread?

Android App Development for Beginners Android AsyncTask going to do background operation on background thread and update on main thread. In android we cant directly touch background thread to main thread in android development. asynctask help us to make communication between background thread to main thread.


2 Answers

There is a pretty good example right on the AsyncTask doc page.

Pass your GMailSender object in to an AsyncTask, and call GMailSender#sendMail during doInBackground.

That is,

public void onClick(View v) {
    final GMailSender sender = new GMailSender("[email protected]", "password");
    new AsyncTask<Void, Void, Void>() {
        @Override public Void doInBackground(Void... arg) {
            try {   
                sender.sendMail("This is Subject",   
                    "This is Body",   
                    "[email protected]",   
                    "[email protected]");   
            } catch (Exception e) {   
                Log.e("SendMail", e.getMessage(), e);   
            } 
        }
    }.execute();

}
like image 179
dokkaebi Avatar answered Nov 06 '22 04:11

dokkaebi


public void onClick(View v) {
final GMailSender sender = new GMailSender("[email protected]",       "password");
new AsyncTask<Void, Void, Void>() {
    @Override public Void doInBackground(Void... arg) {
        try {   
            sender.sendMail("This is Subject",   
                "This is Body",   
                "[email protected]",   
                "[email protected]");   
        } catch (Exception e) {   
            Log.e("SendMail", e.getMessage(), e);   
        } 
    return null;}
}.execute();

}

Thank you "dokkaebi"

like image 21
AwesomeUserName Avatar answered Nov 06 '22 04:11

AwesomeUserName