如何将toast设置为消息发送而不是在Java应用程序的JavaMail中发送

时间:2013-09-29 10:11:25

标签: android javamail

我希望在邮件发送成功时向“发送消息”致敬, 当恶意发送不成功时,则为“未发送消息”

怎么做...谢谢

class RetreiveFeedTask extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... urls) {
        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("example@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(recpient));
            message.setSubject(subject);
            message.setReplyTo(InternetAddress
                    .parse("example@gmail.com"));
            message.setContent(
                    textmessage,
                    "text/html; charset=utf-8");

            Transport.send(message);

        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String feed) {
        pdialog.dismiss();
        reciept.setText("");
        msg.setText("");
        sub.setText("");
        Toast.makeText(getApplicationContext(), "Message sent",
                Toast.LENGTH_LONG).show();

    }
}

2 个答案:

答案 0 :(得分:0)

String中返回的doInBackground是您在onPostExecute中获得的参数。

当一切正常时,您可以返回null,如果发生任何e.getMessage(),则可以Exception。然后,onPostExecute会在feednull时显示确认状态,或者错误不是。

答案 1 :(得分:0)

创建处理程序

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg)
    {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
};

和变量private String message = "" 并在doInBackground相应地更改其值,即message="Mail Sent";message="Mail Not Sent";,并在您想要展示吐司时调用handler.sendEmptyMessage(0);