我正在使用之前的Stack Overflow回复here
中的代码它在android 2.2,2.3中完美运行
但它不适用于4.1及以上版本 - 任何人都可以帮我解决问题吗?
错误报告
08-19 16:18:23.722: E/SEND(17510): ERROR
08-19 16:18:23.722: E/SEND(17510): android.os.NetworkOnMainThreadException
08-19 16:18:23.722: E/SEND(17510): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-19 16:18:23.722: E/SEND(17510): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-19 16:18:23.722: E/SEND(17510): at java.net.InetAddress.getLocalHost(InetAddress.java:365)
08-19 16:18:23.722: E/SEND(17510): at javax.mail.internet.InternetAddress.getLocalAddress(InternetAddress.java:517)
08-19 16:18:23.722: E/SEND(17510): at javax.mail.internet.UniqueValue.getUniqueMessageIDValue(UniqueValue.java:99)
08-19 16:18:23.722: E/SEND(17510): at javax.mail.internet.MimeMessage.updateMessageID(MimeMessage.java:2054)
08-19 16:18:23.722: E/SEND(17510): at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2076)
08-19 16:18:23.722: E/SEND(17510): at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042)
08-19 16:18:23.722: E/SEND(17510): at javax.mail.Transport.send(Transport.java:117)
08-19 16:18:23.722: E/SEND(17510): at hai.bt.expensemanager.Mail.send(Mail.java:77)
08-19 16:18:23.722: E/SEND(17510): at hai.bt.expensemanager.ForgotPasswordActivity$2.onClick(ForgotPasswordActivity.java:79)
08-19 16:18:23.722: E/SEND(17510): at android.view.View.performClick(View.java:4084)
08-19 16:18:23.722: E/SEND(17510): at android.view.View$PerformClick.run(View.java:16966)
08-19 16:18:23.722: E/SEND(17510): at android.os.Handler.handleCallback(Handler.java:615)
08-19 16:18:23.722: E/SEND(17510): at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 16:18:23.722: E/SEND(17510): at android.os.Looper.loop(Looper.java:137)
08-19 16:18:23.722: E/SEND(17510): at android.app.ActivityThread.main(ActivityThread.java:4874)
08-19 16:18:23.722: E/SEND(17510): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 16:18:23.722: E/SEND(17510): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 16:18:23.722: E/SEND(17510): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-19 16:18:23.722: E/SEND(17510): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-19 16:18:23.722: E/SEND(17510): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
使用此代码段:
// mail function
private void mail() {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL,
new String[] { "abc@xyz.in" });
i.putExtra(Intent.EXTRA_SUBJECT, "ENQUERY");
i.putExtra(Intent.EXTRA_TEXT, "");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ContactUsActivity.this,
"Email can't be sent due to device problem",
Toast.LENGTH_SHORT).show();
}
}
它对我有用!
您很可能收到错误,因为您没有将电子邮件作为异步任务发送。 ICS及以上版本不允许在主线程中进行交易!