如果发送邮件,则在Async任务之后启动意图

时间:2014-09-04 05:44:13

标签: android email android-intent asynchronous task

如何在onPostExecute中向用户显示意图或祝酒消息或... 如果电子邮件已发送。显示带有消息(发送电子邮件)的新窗口的用户。

这是我的班级反馈表

    private void sendMail(String email, String subject, String messageBody) {
    Session session = createSessionObject();
    try {
        Message message = createMessage(email, subject, messageBody, session);
        new SendMailTask().execute(message);
    } catch (AddressException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}
private Message createMessage(String email, String subject, String messageBody, Session session) throws MessagingException, UnsupportedEncodingException {
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("NicerDicer@big.com", "report for APP"));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
    message.setSubject(subject);
    message.setText(messageBody);

    return message;
}
private Session createSessionObject() {
    Properties properties = new Properties();
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");
    return Session.getInstance(properties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });
}
private class SendMailTask extends AsyncTask<Message, Void, Void> {
    private ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = ProgressDialog.show(AboutPage.this, "Please wait", "Sending mail", true, false);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        progressDialog.dismiss();

    }
    @Override
    protected Void doInBackground(Message... messages) {
        try {
            Transport.send(messages[0]);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return null;
    }

1 个答案:

答案 0 :(得分:0)

Irt很容易失败,

这个

的AsyncTask calss的Oveload构造函数
SendMailTask(Context context){

}

在您的班级中有一个私人语境成员

private class SendMailTask extends AsyncTask<Message, Void, Void> {
  private Context context;

现在执行后,只需执行此操作。

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    progressDialog.dismiss();
    Toast.makeText(context,"My Message",Toast.LENGTH_SHORT).show();

}

并且不要忘记将此方法签名更改为此。

private void sendMail(Context context,String email, String subject, String messageBody)
希望这足够了..