等待try-catch完成后再继续(Javamail身份验证)

时间:2015-04-02 20:05:28

标签: android multithreading passwords try-catch javamail

所以这就是问题,我需要通过执行发送操作来检查密码用户插入是否适合他的Gmail。但是应用程序必须从谷歌服务器捕获异常,因此它可以提升标志然后主活动将根据该标志(结果字符串)行动。但有些应用程序不会等待try catch块完成,因此它会在标志更改之前通过所有代码行。

public class MailSender {
    String author = null;
    String address = null;
    String subject = null;
    String textMessage = null;
    String result = null;
    Session session = null;
    protected String checkPass(final String myEmail, final String myPass) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    session = Session.getDefaultInstance(props, new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(myEmail, myPass);
        }
    });
    CheckPass task = new CheckPass();
    task.execute();
    return result;
}
public void getResult(String result)
{
    this.result = result;
}
class CheckPass extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        String myResult = null;
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("p2pteamtaskmanagement@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("p2pteamtaskmanagement@gmail.com"));
            message.setSubject("test check pass");
            message.setContent("message", "text/html; charset=utf-8");
            Transport.send(message);                
        } catch (AuthenticationFailedException e) {
            e.printStackTrace();
            myResult =  "Wrong password";
            return myResult;
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        myResult =  "ok";
        return myResult;
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        getResult(result);
    }
}
}

这是主要活动的一部分

reg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (eName.getText().toString().trim().isEmpty()) {
                Toast.makeText(getApplicationContext(), "Please enter password", Toast.LENGTH_SHORT).show();
                return;
            } else {
                MailSender myMailSender = new MailSender();
                String test = myMailSender.checkPass(tUser.getText().toString(), eName.getText().toString());
                if (test == "Wrong password") {
                    Toast.makeText(getApplicationContext(), "Wrong password", Toast.LENGTH_SHORT).show();
                    return;
                }
                MyAsync ma = new MyAsync();
                ma.execute();
                intent.putExtra(ACCOUNT_INTENT, tUser.getText().toString());
                intent.putExtra(NAME_INTENT, eName.getText().toString());
                startActivity(intent);
            }
        }
    });

如果有更简单的方法可以使用Javamail检查gmail密码是否正确,或有人帮助我。

感谢和抱歉我的英语以及我的代码方式。

1 个答案:

答案 0 :(得分:1)

AsyncTask用于在后台执行异步。这意味着您执行任务的线程将在启动任务后继续执行。在检查身份验证是否成功之前,您将等待任务完成。