Need Help Debugging Email Code

僤鯓⒐⒋嵵緔 提交于 2019-12-24 22:14:55

问题


I was wondering if someone could help me with debugging. I am currently using the code in:

Sending Email in Android using JavaMail API without using the default/built-in app

and have used all of the code as directed. After inputting my email fields, I was unable to get email to be sent. I noticed a piece of code in GMailSender.java, where it seemed that there was a missing piece of code.

}catch(Exception e){

}

Knowing that I had debugged every other part of the code, I added a Log.e, as so.

}catch(Exception e){
    Log.e("GmailDebug", e.getMessage(), e); 
}

As a result, I noticed that I got the following logs.

01-16 22:22:38.933: E/GmailDebug(4487): null
01-16 22:22:38.933: E/GmailDebug(4487): android.os.NetworkOnMainThreadException

After commenting out the lines beforehand and systematically uncommenting them, I finally was able to determine that I didn't get any logs until I uncommented the line

Transport.send(message);

Can someone help me out, and tell me how I can prevent these errors? Thanks!


回答1:


This happens because you are doing a network operation on the main thread, and this is not allowed on Android 3.0 and above. Even though it is in a service, services are run on the UI thread unless you specifically launch them in another thread or create a thread inside it.

You can fix this by running the task in a background thread off the main UI thread, by using a Thread or an AsyncTask.



来源:https://stackoverflow.com/questions/14373468/need-help-debugging-email-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!