从app发送电子邮件的android不再有效

时间:2016-06-29 10:21:46

标签: java android gmail

我使用this教程从我的应用内发送电子邮件。 我使用相同的代码到3个不同的应用程序,这一切都很好。但现在,几个月后,它停止了工作。 我搜索了所有可能的网站,但没有一个帮助过我。 我尝试从我的Gmail帐户设置较低的安全性,并且还更改了我的会话的属性,但没有任何效果。 我真的很感激一些帮助。

            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);
            }

我的错误:

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

1 个答案:

答案 0 :(得分:0)

来自日志:

  

java.net.ConnectException:90000ms后无法连接到smtp.gmail.com/2a00:1450:4013:c01::6d(端口465):isConnected失败:ENETUNREACH(网络无法访问)

网络无法访问。代码看起来没问题,因此问题必须与网络有关。请检查您的连接。可能的原因可能是:

  1. 非常慢/坏的网络。尝试更改网络。
  2. 清单中没有Internet权限(这不应该是您的问题所描述的情况)。添加权限。
  3. 将来,您可以在发送网络请求之前添加网络检查。此线程有一些检查网络连接的好答案:How to check internet access on Android? InetAddress never times out