0

I read guides for javamail-android and some posts on stackoverflow. E.g. How do you send mail in Android using JavaMail API? I get message "Mail send successfully...". But mail does not comes to reciever addresses. And I have following output:

02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ android.os.NetworkOnMainThreadException
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at java.net.InetAddress.getByName(InetAddress.java:289)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.URLName.getHostAddress(URLName.java:487)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.URLName.hashCode(URLName.java:463)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at java.util.Collections.secondaryHash(Collections.java:3405)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at java.util.Hashtable.get(Hashtable.java:265)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.Session.getPasswordAuthentication(Session.java:823)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.Service.connect(Service.java:271)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.Service.connect(Service.java:169)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.Service.connect(Service.java:118)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.Transport.send0(Transport.java:188)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.Transport.send(Transport.java:118)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at com.example.mailapp.GMailSender.sendMail(GMailSender.java:64)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at com.example.mailapp.MyActivity$1.onClick(MyActivity.java:35)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at android.view.View.performClick(View.java:4438)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

How can I fix it or use another way to send email?

Community
  • 1
  • 1
Vladimir Solovyov
  • 287
  • 1
  • 2
  • 11

2 Answers2

0

You are getting this error because you are performing a network operation on the main thread, network operations take unprecedented time and should not be called on main thread. Use Async task for it. Async task will create another thread to perform the network task(you can use it for any task too).

Add your code to perform the operation in doInBackground method and call it using below:

   new senmailAsync().execute();

Eg:

private class senmailAsync extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            GMailSender gMailSender = new GMailSender();
            gMailSender.sendMail("hi", "hi", authPreferences.getUser(), authPreferences.getToken(), "ranjithdevacc@gmail.com");
            Log.v("ranjapp", "sent mail " + authPreferences.getUser() + "  " + authPreferences.getToken());
            return null;
        }
    }

Complete tutorial here

Psypher
  • 10,717
  • 12
  • 59
  • 83
0

You also can be need to pass parameters into the class. You can do it like this:

public class MailSenderAsyncClass extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        MailSenderClass sender = new MailSenderClass(Settings.Login, Settings.Pass);
        try {
            sender.sendMail(params[2],params[3],params[0],params[1],params[4]);
        } catch (Exception e) {
            Log.e("SendMail", e.getMessage(), e);
            e.printStackTrace();
        }

        return null;
    }
}

new MailSenderAsyncClass().execute(Settings.EmailFrom, Settings.EmailTo, "Subject", "text of mail", Settings.AnotherParam);