简体   繁体   English

需要帮助调试电子邮件代码

[英]Need Help Debugging Email Code

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 使用JavaMail API在Android中发送电子邮件,而无需使用默认/内置应用

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. 我注意到GMailSender.java中有一段代码,似乎缺少一部分代码。

}catch(Exception e){

}

Knowing that I had debugged every other part of the code, I added a Log.e, as so. 知道我已经调试了代码的所有其他部分,因此添加了Log.e。

}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! 谢谢!

This happens because you are doing a network operation on the main thread, and this is not allowed on Android 3.0 and above. 发生这种情况是因为您正在主线程上执行网络操作,而在Android 3.0及更高版本上则不允许这样做。 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. 即使它在服务中,服务也要在UI线程上运行,除非您专门在另一个线程中启动它们或在其中创建线程。

You can fix this by running the task in a background thread off the main UI thread, by using a Thread or an AsyncTask . 您可以通过使用ThreadAsyncTask在主UI线程之外的后台线程中运行任务来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM