-2

我在这里使用以前的 Stack Overflow 答案中的代码

它在 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)
4

1 回答 1

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 及以上不允许在主线程中进行事务!

于 2013-08-19T08:58:11.937 回答