使用的AsyncTask发送电子邮件的Android [英] Using AsyncTask to Send Android Email
问题描述
我最近问关于以下code的一个问题:
<一个href="http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a">Sending电子邮件在Android中使用JavaMail API不使用默认/内置应用
我在关于网络错误问这个,因为每一个previous问题:
我的问题是,我怎么会为了成功地发送邮件与这个Android code实现一个AsyncTask的?每一个教程,我看到告诉我,我应该怎么办
扩展AsyncTask的{
不过,GMailSender.java已经有此定义为:
公共类GMailSender扩展javax.mail.Authenticator
会有人能帮助我吗?谢谢!
请注意:
请不要喜欢谁了-1 版这个问题,并公布了确切的答案在<一个被赋予了白痴href="http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a">Sending电子邮件在Android中使用JavaMail API不使用默认/内置应用。我无法使用了精确的编码,由于这样的事实,它是不再可能一个Android应用程序的主线程上运行的网络操作。我正在寻找一种方式来使用的AsyncTask,以便在后台运行操作。什么我无法找出是怎么做的。
扩展AsyncTask的{
不接触
公共类GMailSender扩展javax.mail.Authenticator
有一个pretty的很好的例子,对上的 的AsyncTask
文档页面。
通过你的 GMailSender
对象中的的AsyncTask
,并调用 GMailSender#sendmail的
在 doInBackground
。
也就是说,
公共无效的onClick(视图v){
最后GMailSender发件人=新GMailSender(username@gmail.com,密码);
新的AsyncTask&LT;虚空,虚空,虚空&GT;(){
@覆盖公共无效doInBackground(空... ARG){
尝试 {
sender.sendMail(这是主题
这是身体,
user@gmail.com
user@yahoo.com);
}赶上(例外五){
Log.e(Sendmail的,e.getMessage(),E);
}
}
}。执行();
}
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屋!