我希望在邮件发送成功时向“发送消息”致敬, 当恶意发送不成功时,则为“未发送消息”
怎么做...谢谢
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();
}
}
答案 0 :(得分:0)
String
中返回的doInBackground
是您在onPostExecute
中获得的参数。
当一切正常时,您可以返回null
,如果发生任何e.getMessage()
,则可以Exception
。然后,onPostExecute
会在feed
为null
时显示确认状态,或者错误不是。
答案 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);