我正在创建一个应用程序,它通过EditText和Checkbox接受一些用户输入,并将它们放入电子邮件的主题和正文中。
每当我运行并尝试在try块中设置主题时,我的电子邮件都不会发送,我会收到这些LogCat错误。
public void sendEmails() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... arg0) {
String subject = "Maintenance Request";
CheckBox main = (CheckBox) findViewById(R.id.checkBox1);
CheckBox kitchen = (CheckBox) findViewById(R.id.checkBox2);
CheckBox bathroom = (CheckBox) findViewById(R.id.checkBox3);
CheckBox other = (CheckBox) findViewById(R.id.checkBox4);
String host = "smtp.gmail.com";
String username = "user@gmail.com";
String password = "pwd";
Properties props = new Properties();
props.put("mail.smtp.ssl.enable", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
MimeMessage msg1 = new MimeMessage(session);
try {
if (main.isChecked()) {
subject = subject + " - Main Room";
}
if (kitchen.isChecked()) {
subject = subject + " - Kitchen";
}
if (bathroom.isChecked()) {
subject = subject + " - Bathroom";
}
if (other.isChecked()) {
subject = subject + " - Other";
}
return subject;
}
msg.setFrom(new InternetAddress(username));
msg1.setFrom(new InternetAddress(username));
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
msg1.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(manager));
msg.setSubject("Maintenance Confirmation");
msg1.setSubject(subject);
msg.setText("Some really important stuff. Confirmed.");
msg1.setText("Really important stuff needs attention");
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.quitwait", "false");
Transport t = session.getTransport("smtps");
try {
t.connect(host, username, password);
t.sendMessage(msg, msg.getAllRecipients());
t.sendMessage(msg1, msg1.getAllRecipients());
}
finally {
t.close();
} }
catch(Exception exc){
exc.printStackTrace();
}
return null;
}}.execute(); }
点击按钮,会运行并调用代码
public void buttonclick3(View v) {
//first extract text for EditText and convert to String
EditText e = (EditText) findViewById(R.id.enterEmail);
String email = e.getText().toString();
//run validateEmail on String and show alert if format is invalid
if (validateEmail(email) == false) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Please enter a valid Email address.");
builder.setTitle("Invalid Input!");
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.show();
}
else {
//the email address is valid, send the emails
sendEmails();
}
我知道代码很好,因为如果我对主题进行硬编码而不调用该方法,那么电子邮件就可以正常发送。
编辑:这是LogCat。应用程序不再在按下最后一个按钮时崩溃,但电子邮件不发送,我在LogCat中以黄色显示这些错误,而不是红色。谁能理解我的线程在做什么并提供解决方案?
答案 0 :(得分:0)
我认为这里的问题是您尝试访问AsyncTask
后台线程中的UI。这个线程无法访问UI线程,很容易导致这样的问题。
您需要将与用户界面相关的所有代码从doInBackground()
方法移至AsyncTask
的另一个方法覆盖,例如onPreExecute()
方法,或者您可以将其移至之前任务已运行以获得相同的结果。例如:
private String subject = "Maintenance Request";
@Override
protected void onPreExecute() {
CheckBox main = (CheckBox) findViewById(R.id.checkBox1);
CheckBox kitchen = (CheckBox) findViewById(R.id.checkBox2);
CheckBox bathroom = (CheckBox) findViewById(R.id.checkBox3);
CheckBox other = (CheckBox) findViewById(R.id.checkBox4);
if (main.isChecked()) subject = subject + " - Main Room";
if (kitchen.isChecked()) subject = subject + " - Kitchen";
if (bathroom.isChecked()) subject = subject + " - Bathroom";
if (other.isChecked()) subject = subject + " - Other";
}
或者您可以将其移至sendEmails();
方法的buttonclick3()
行之前。
它没有发送电子邮件,因为发生了错误,并且在发送电子邮件之前终止了try / catch中代码的进一步执行。
你还应该考虑更多地了解AsyncTask,我发现这是非常有益的:http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html
回答原始问题:
这可能会发生,因为您已在单独的方法中定义了subject
变量,因此subject
变量超出了范围。您的setSubject()
方法会返回subject
变量的值,因此要解决此问题,您可以从点击侦听器中删除setSubject()
方法调用并更改此行:
msg1.setSubject(subject);
对此:
msg1.setSubject(setSubject());