简体   繁体   English

android从应用程序发送电子邮件不再起作用

[英]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. 我对3个不同的应用程序使用了相同的代码,并且都运行良好。 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. 我尝试通过我的Gmail帐户设置较低的安全性,并更改了会话的属性,但没有任何效果。 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

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) java.net.ConnectException:90000ms后无法连接到smtp.gmail.com/2a00:1450:4013:c01::6d(端口465):isConnected失败:ENETUNREACH(网络不可达)

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). 清单中没有Internet权限(问题所描述的情况不应该如此)。 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? 此线程为检查网络连接提供了一些很好的答案: 如何在Android上检查互联网访问? InetAddress never times out InetAddress永不超时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM