我有以下 JavaMail 实现,它在 Android 2.2 (Froyo) 模拟器上运行良好(该项目的目标设置为最新版本 17)。当我在 Galaxy SIII 手机(安装了 Android 4.1.1)上加载它时,应用程序在“catch”上崩溃,由 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();
}
}