我试过这个link通过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)
答案 0 :(得分:1)
android.os.NetworkOnMainThreadException
那里有你的答案。您正尝试在主UI线程中访问网络。这是不好的!有关如何避免此问题的指导,请参阅this article。
您链接到的文章是通过在单击处理程序中调用send()
方法设置错误示例。您可能需要联系该页面的作者以指出问题。
答案 1 :(得分:1)
您正尝试在主线程上执行与网络相关的操作!! Android 3.0以上版本根本不允许这样做,作为制作更好的行为和响应式应用程序的一步(旧版本只是发出了警告)。请参阅stackTrace中的这一行
android.os.NetworkOnMainThreadException
因此,您需要将电子邮件发送部分从主线程推出。在您所遵循的示例中,不需要AsyncTask,只需要一个简单的线程。
Runnable r = new Runnable() {
@Override
public void run() {
// send email
Transport.send(msg);
}
};
Thread t = new Thread(r);
t.start();