使用AsyncTask发送Android电子邮件 [英] Using AsyncTask to Send Android Email

查看:149
本文介绍了使用AsyncTask发送Android电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问过关于以下代码的问题:



使用JavaMail API在Android中发送电子邮件,而不使用默认/内置应用



根据以前的问题,我已经询问过网络错误:



需要帮助调试电子邮件代码



我的问题是,怎么我实现一个AsyncTask,以便成功发送一个这个Android代码的电子邮件?我看到的每个教程都告诉我,我应该做

  extends AsyncTask {
/ pre>

但是,GMailSender.java已经定义为:

  public class GMailSender extends javax.mail.Authenticator 

有人能帮助我吗?谢谢!



注意:



请不要像笨蛋一样有-1 ed 这个问题,并发布了确切的答案,如使用JavaMail API在Android中发送电子邮件,而不使用默认/内置应用。由于不可能在Android应用程序的主线程上运行网络操作,因此无法使用精确的编码。我正在寻找一种使用AsyncTask的方法,以便在后台运行该操作。我无法找出是如何做的

 扩展AsyncTask {
/ pre>

不触摸

  public class GMailSender extends javax.mail .Authenticator 


解决方案

有一个很好的例子 AsyncTask doc页面



将您的 GMailSender 对象传递给 AsyncTask 并在 doInBackground 中调用 GMailSender#sendMail



是,

  public void onClick(View v){
final GMailSender sender = new GMailSender(username @ gmail。 com,密码);
new AsyncTask< Void,Void,Void>(){
@Override public Void doInBackground(Void ... arg){
try {
sender.sendMail(This is主题,
This is Body,
user@gmail.com,
user@yahoo.com);
} catch(Exception e){
Log.e(SendMail,e.getMessage(),e);
}
}
} .execute();

}


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

解决方案

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("username@gmail.com", "password");
    new AsyncTask<Void, Void, Void>() {
        @Override public Void doInBackground(Void... arg) {
            try {   
                sender.sendMail("This is Subject",   
                    "This is Body",   
                    "user@gmail.com",   
                    "user@yahoo.com");   
            } catch (Exception e) {   
                Log.e("SendMail", e.getMessage(), e);   
            } 
        }
    }.execute();

}

这篇关于使用AsyncTask发送Android电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆