Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
850 views
in Technique[技术] by (71.8m points)

debugging - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...