In order to help those getting a Network On Main Thread Exception with an SDK Target >9. This is using droopie's code above but will work similarly for any.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();StrictMode.setThreadPolicy(policy); android.os.NetworkOnMainThreadException
You can use AsyncTask as below
public void onClickMail(View view) { new SendEmailAsyncTask().execute();}class SendEmailAsyncTask extends AsyncTask <Void, Void, Boolean> { Mail m = new Mail("from@gmail.com", "my password"); public SendEmailAsyncTask() { if (BuildConfig.DEBUG) Log.v(SendEmailAsyncTask.class.getName(), "SendEmailAsyncTask()"); String[] toArr = { "to mail@gmail.com"}; m.setTo(toArr); m.setFrom("from mail@gmail.com"); m.setSubject("Email from Android"); m.setBody("body."); } @Override protected Boolean doInBackground(Void... params) { if (BuildConfig.DEBUG) Log.v(SendEmailAsyncTask.class.getName(), "doInBackground()"); try { m.send(); return true; } catch (AuthenticationFailedException e) { Log.e(SendEmailAsyncTask.class.getName(), "Bad account details"); e.printStackTrace(); return false; } catch (MessagingException e) { Log.e(SendEmailAsyncTask.class.getName(), m.getTo(null) +"failed"); e.printStackTrace(); return false; } catch (Exception e) { e.printStackTrace(); return false; } }