我有以下JavaMail实现,它在Android 2.2(Froyo)模拟器上工作正常(项目的目标设置为版本17,最新)。当我将它加载到Galaxy SIII手机(安装了Android 4.1.1)时,应用程序通过LogCat指示在'catch'上崩溃;甚至没有显示捕获内的Toast。任何提示? 这是代码:
/**
* Send an Email with a file in attach (the file is located in SDCARD/mailattach/ directory)
* @param emailAddress
* @param emailObject
* @param emailBody
* @param attachedFile name
*/
public static void sendEmail(String emailAddress, String emailSubject, String emailBody, String attachedFile) {
Email m = new Email(email_config.get("ACCOUNT_NAME"), email_config.get("ACCOUNT_PASSWORD"));
String[] toArr = { emailAddress };
// Email server static initializations
m.setHost(email_config.get("DEFAULT_SMTP_SERVER"));
m.setPort(email_config.get("DEFAULT_SMTP_PORT"));
if (email_config.get("SMTP_AUTHENTICATION") == "true") {
m.setSauth(true);
} else {
m.setSauth(false);
}
m.setSport(email_config.get("SOCKET_FACTORY_PORT"));
if (email_config.get("DEBUG_MODE") == "true") {
m.setDebug(true);
} else {
m.setDebug(false);
}
// Email transmitted parameters
m.setTo(toArr);
m.setFrom(email_config.get("ACCOUNT_NAME"));
m.setSubject(emailSubject);
m.setBody(emailBody);
String attachPath = Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + "mailattach" + File.separator + attachedFile;
try {
// set the attachment and send
m.addAttachment(attachPath);
if(m.send()) {
Toast.makeText(context, "Email was sent successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Email was not sent.", Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
Toast.makeText(context, "Error sending the email: "+e.toString(), Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
确保不在主线程中调用网络,因为Android 4不允许它。