2012-03-16 67 views
0

電子郵件我想這link通過Android 3.2的發送電子郵件,但我得到這個錯誤:不能發送在Android 3.2

01-11 13:48:59.818: E/MailApp(29958): Could not send email 
01-11 13:48:59.818: E/MailApp(29958): android.os.NetworkOnMainThreadException 
01-11 13:48:59.818: E/MailApp(29958): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084) 
01-11 13:48:59.818: E/MailApp(29958): at java.net.InetAddress.lookupHostByName(InetAddress.java:391) 
01-11 13:48:59.818: E/MailApp(29958): at java.net.InetAddress.getLocalHost(InetAddress.java:371) 
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.InternetAddress.getLocalAddress(InternetAddress.java:517) 
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.UniqueValue.getUniqueMessageIDValue(UniqueValue.java:99) 
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.MimeMessage.updateMessageID(MimeMessage.java:2054) 
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2076) 
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042) 
01-11 13:48:59.818: E/MailApp(29958): at javax.mail.Transport.send(Transport.java:117) 
01-11 13:48:59.818: E/MailApp(29958): at com.fahadalawam.kwtresturant.Mail.send(Mail.java:125) 
01-11 13:48:59.818: E/MailApp(29958): at com.fahadalawam.kwtresturant.Add$1.onClick(Add.java:58) 
01-11 13:48:59.818: E/MailApp(29958): at android.view.View.performClick(View.java:3480) 
01-11 13:48:59.818: E/MailApp(29958): at android.view.View$PerformClick.run(View.java:13983) 
01-11 13:48:59.818: E/MailApp(29958): at android.os.Handler.handleCallback(Handler.java:605) 
01-11 13:48:59.818: E/MailApp(29958): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-11 13:48:59.818: E/MailApp(29958): at android.os.Looper.loop(Looper.java:137) 
01-11 13:48:59.818: E/MailApp(29958): at android.app.ActivityThread.main(ActivityThread.java:4340) 
01-11 13:48:59.818: E/MailApp(29958): at java.lang.reflect.Method.invokeNative(Native Method) 
01-11 13:48:59.818: E/MailApp(29958): at java.lang.reflect.Method.invoke(Method.java:511) 
01-11 13:48:59.818: E/MailApp(29958): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
01-11 13:48:59.818: E/MailApp(29958): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
01-11 13:48:59.818: E/MailApp(29958): at dalvik.system.NativeStart.main(Native Method) 

回答

1
android.os.NetworkOnMainThreadException 

有你的答案就在這裏。您正試圖在主UI線程中訪問網絡。這不好!有關如何避免此問題的指導,請參閱this article

您鏈接到的文章通過在點擊處理程序中調用send()方法來設置一個不好的示例。您可能需要聯繫該頁面的作者以指出問題。

+0

非常感謝你,我用AsyncTask解決了這個問題。 – 2012-03-16 18:23:12

1

您正嘗試在主線程上執行網絡相關操作! Android 3.0以上版本不允許將它作爲實現更好性能和響應應用程序的一個步驟(舊版本僅發出警告)。在你的stackTrace中看到這一行

android.os.NetworkOnMainThreadException 

所以,你需要將電子郵件發送部分從主線程中推出。在你所關注的例子中,不需要一個AsyncTask,一個簡單的Thread就可以做到。

Runnable r = new Runnable() { 

     @Override 
     public void run() { 
       // send email 
        Transport.send(msg); 

     } 
    }; 
     Thread t = new Thread(r); 
     t.start();