Search code examples
javaandroidgmail

android sending email from app no longer working


I used this tutorial to send emails from inside my app. I used the same code to 3 different apps and it all worked well. But now, a few months later, it stopped working. I have searched all the possible sites, but none of them helped me. I tried setting a lower security from my Gmail account and also changing the properties for my session, but nothing worked. I would really appreciate some help.

            Log.d("EmailSender","sending message");
            Properties props = new Properties();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");
            Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user,pass);
                }
            });
            try {
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress(user));
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(SharedPreferenceUtils.getUserEmail(getActivity())));
                message.setSubject(subject);
                message.setContent(content, "text/html; charset=utf-8");

                //Transport.send(message);
                Transport transport = session.getTransport("smtps");
                transport.connect (host, Integer.parseInt(port), user, pass);
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
                Log.d("EmailSender", "message sent");
            } catch (MessagingException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }

My error:

java.net.ConnectException: failed to connect to smtp.gmail.com/2a00:1450:4013:c01::6d (port 465) after 90000ms: isConnected failed: ENETUNREACH (Network is unreachable)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
    at javax.mail.Service.connect(Service.java:288)
    at com.mihaela.myapp.ui.dialog.EnterPasswordDialog$SendMailTask.doInBackground(EnterPasswordDialog.java:222)
    at com.mihaela.myapp.ui.dialog.EnterPasswordDialog$SendMailTask.doInBackground(EnterPasswordDialog.java:179)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)
     Caused by: java.net.ConnectException: failed to connect to smtp.gmail.com/2a00:1450:4013:c01::6d (port 465) after 90000ms: isConnected failed: ENETUNREACH (Network is unreachable)
    at libcore.io.IoBridge.isConnected(IoBridge.java:267)
    at libcore.io.IoBridge.connectErrno(IoBridge.java:191)
    at libcore.io.IoBridge.connect(IoBridge.java:127)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:461)
    at java.net.Socket.connect(Socket.java:918)
    at java.net.Socket.connect(Socket.java:844)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
        ... 10 more
     Caused by: android.system.ErrnoException: isConnected failed: ENETUNREACH (Network is unreachable)
    at libcore.io.IoBridge.isConnected(IoBridge.java:252)
        ... 19 more

Solution

  • From the logs:

    java.net.ConnectException: failed to connect to smtp.gmail.com/2a00:1450:4013:c01::6d (port 465) after 90000ms: isConnected failed: ENETUNREACH (Network is unreachable)

    Network is unreachable. The code looks okay, therefore the issue must be with the network. Please check your connectivity. Possible causes could be:

    1. Very slow/ bad network. Try changing the network.
    2. Internet permission not available in the manifest (this shouldn't be the case as described by your problem). Add the permission.

    For future, you can add a network-check before sending network requests. This thread has some good answers for checking the network connectivity: How to check internet access on Android? InetAddress never times out