The Obscure Question
The Obscure Question

Reputation: 1174

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

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!

Upvotes: 0

Views: 245

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82563

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.

Upvotes: 1

Related Questions