以下是我的代码,
public class SendMail extends AsyncTask<Void,Void,Void> {
private Context context;
private Session session;
private String email;
private String subject;
private String message;
private ProgressDialog progressDialog;
public SendMail(Context context,String email, String subject,String message){
this.context = context;
this.email = email;
this.subject = subject;
this.message = message;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(context,"Sending Email","Please Wait...",false,false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
Toast.makeText(context,"Email Sent", Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... voids) {
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 javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new
PasswordAuthentication(Config.EMAIL, Config.PASSWORD);
}
});
try {
MimeMessage mm = new MimeMessage(session);
mm.setFrom(new InternetAddress(Config.EMAIL));
mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
mm.setSubject(subject);
mm.setText(message);
Transport.send(mm);
}catch (MessagingException e){
e.printStackTrace();
}
return null;
}
}
我知道我得到一个错误,因为ProgressDialog正在运行而我的Activity被销毁。我应该放一个progressDialog.dismiss()。所以我把它但是仍然,我收到了这个错误。
我不知道如何解决这个问题。希望有人帮助我。