【问题标题】:JavaMail API not working on Mobile Data but fine on WifiJavaMail API 不适用于移动数据,但适用于 Wifi
【发布时间】:2016-12-14 11:40:35
【问题描述】:

我现在的情况很奇怪。完整代码在这里可以看到:http://javapapers.com/android/android-email-app-with-gmail-smtp-using-javamail/

我将此代码用于发送电子邮件。但问题就在眼前。在 WIFI 上使用此方法发送邮件时,我有 100% 的成功率。但是当我使用移动数据(4G)时,我的成功率约为 30%。在某些设备上,它根本不起作用。 4G连接没有问题。它没有波动。它工作正常。我在多个设备上尝试了这个 Wi-Fi 和移动数据。一切都表明移动数据上的代码有问题。

目前在我的移动数据调试设备上,代码正在运行。因此我现在不能发布 logcat。但很多时候它甚至在调试设备上都不起作用。但是现在因为它正在工作,我不能发布 logcat。很可能,这部分代码存在一些问题:

 try {
        Transport transport = mailSession.getTransport("smtp");
        transport.connect(emailHost, fromEmail, fromPassword);
        Log.i("GMail", "allrecipients: " + emailMessage.getAllRecipients());
        transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
        transport.close();
        Log.i("GMail", "Email sent successfully.");
    }catch (Exception e){

    }

在阅读其他人的帖子时,我知道代理设置可能存在问题。但是在代码中,我不知道在哪里可以使用Proxy.NoProxy 之类的东西。完整的代码可在上面的链接中找到。我没有修改它(除了添加我自己的电子邮件和密码)。

这是失败的logcat:

 08-09 19:29:54.594 9551-9551/com.parassidhu.cdlumaths I/SendMailActivity:点击发送按钮。
08-09 19:29:54.594 9551-9551/com.parassidhu.cdlumaths I/SendMailActivity:列表:[cdludeubg@gmail.com]
08-09 19:29:54.654 9551-9893/com.parassidhu.cdlumaths I/d: 即将实例化 GMail...
08-09 19:29:54.668 9551-9893/com.parassidhu.cdlumaths I/GMail:邮件服务器属性设置。

                                                               [08-09 19:29:54.677 9551:9893 W/]
                                                               无法打开“/system/framework/WfdCommon.jar”:没有这样的文件或目录
08-09 19:29:55.000 9551-9893/com.parassidhu.cdlumaths I/GMail: toEmail: cdludebug@gmail.com
08-09 19:29:55.099 9551-9893/com.parassidhu.cdlumaths I/GMail:电子邮件消息已创建。
08-09 19:29:58.393 9551-9893/com.parassidhu.cdlumaths I/错误:sendEmail:空
08-09 19:29:58.393 9551-9893/com.parassidhu.cdlumaths I/com.downloadinformer:邮件已发送。

编辑 2 在另一台设备上,我得到了 logcat:

08-10 17:44:58.911 32384-32384/com.parassidhu.cdlumaths I/SendMailActivity:点击发送按钮。
08-10 17:44:58.911 32384-32384/com.parassidhu.cdlumaths I/SendMailActivity: 列表: [cdludeubg@gmail.com]
08-10 17:44:58.922 32384-1140/com.parassidhu.cdlumaths I/d: 即将实例化 GMail...
08-10 17:44:58.930 32384-1140/com.parassidhu.cdlumaths I/GMail:邮件服务器属性设置。
08-10 17:44:59.027 32384-1140/com.parassidhu.cdlumaths I/GMail: toEmail: cdludebug@gmail.com
08-10 17:44:59.088 32384-1140/com.parassidhu.cdlumaths I/GMail:电子邮件消息已创建。
08-10 17:44:59.095 32384-1140/com.parassidhu.cdlumaths D/libc-netbsd: [getaddrinfo]: hostname=smtp.gmail.com;服务名称=(空);缓存模式=(空),netid=0;标记=0
08-10 17:44:59.095 32384-1140/com.parassidhu.cdlumaths D/libc-netbsd: [getaddrinfo]: hostname=smtp.gmail.com;服务名称=(空);缓存模式=(空),netid=0;标记=0
08-10 17:44:59.208 32384-1140/com.parassidhu.cdlumaths D/libc-netbsd: getaddrinfo: smtp.gmail.com 从代理获取结果 >>
08-10 17:44:59.219 32384-1140/com.parassidhu.cdlumaths I/System.out: [socket][0] 连接 smtp.gmail.com/2404:6800:4003:c00::6c:587;LocalPort =44496(0)
08-10 17:44:59.219 32384-1140/com.parassidhu.cdlumaths I/System.out: [CDS] 连接 [smtp.gmail.com/2404:6800:4003:c00::6c:587] tm:90
08-10 17:45:02.079 32384-1140/com.parassidhu.cdlumaths D/libc-netbsd: [getaddrinfo]: 主机名=smtp.gmail.com;服务名称=(空);缓存模式=(空),netid=0;标记=0
08-10 17:45:02.079 32384-1140/com.parassidhu.cdlumaths D/libc-netbsd: [getaddrinfo]: 主机名=smtp.gmail.com;服务名称=(空);缓存模式=(空),netid=0;标记=0
08-10 17:45:02.083 32384-1140/com.parassidhu.cdlumaths D/libc-netbsd: getaddrinfo: smtp.gmail.com 从代理获取结果 >>
08-10 17:45:02.084 32384-1140/com.parassidhu.cdlumaths I/错误:sendEmail:空

【问题讨论】:

  • 您确实需要在调试输出失败时发布。如果您能够描述失败时与成功时的不同之处,那将会有所帮助。您是否总是使用具有相同用户名和密码的同一邮件服务器?总是同一台设备出现故障还是您使用多个设备?失败与您所在的位置有关吗?
  • 如果我能得到 logcat,让我尝试更多。我正在使用相同的邮件服务器。相同的用户名,相同的密码。有多个设备出现故障。这似乎与位置无关
  • 某些国家/地区的某些提供商会阻止端口。特别是在预算电话合同中,您有时只能打开 http/s 端口。虽然今天并不常见,但几年前我也遇到过类似的问题。
  • 但它在同一部手机上工作,现在突然停止工作。如果我从手机制作热点以将同一部手机连接到互联网,它就可以工作。不能有代理之类的东西吗?我将端口 587 用于 smtp。除了smtp之外,就没有其他发送邮件的方式了吗?
  • 可以发JavaMail debug output吗?

标签: java android email jakarta-mail


【解决方案1】:

这一定与您的邮件服务器上的身份验证有关。仅使用默认的 android 电子邮件应用程序,我仍然无法通过 Mobile Data 访问我的电子邮件服务器,并且它是一个具有奇怪身份验证的旧服务器。仔细检查身份验证设置服务器端。

【讨论】:

  • 这是另一种方法。但我想一键发送选项。比如“竖起大拇指”。然后用户不想打开电子邮件客户端。应该有一键发送选项。你能检查一下代码并告诉我吗?
  • 对不起,只是告诉你我在这个问题上的经历,没有更多的见解......
【解决方案2】:

在我的测试中,我意识到当我将手机的首选网络类型从 4G 更改为 3G 时,它可以正常工作,然后当我从 3G 更改为 4G 时,它仍然可以正常工作。

【讨论】:

  • 我们无法在每个用户的手机上执行此操作
【解决方案3】:

在我的应用程序中我解决了这个问题,我认为这是 smtp.gmail.com 的问题。

解决办法:

创建 Outlook 电子邮件并放置这些配置:

  • SMTP 服务器名称 smtp-mail.outlook.com

  • SMTP 端口 587

  • SMTP 加密方式 TLS

        try {
    
        String prot = "smtp";
        final String user = "MyEmailAdress@outlook.com";
        String to = "anotherEmail@outlook.com";
        final String pass = "MYpassword";
        String mailhost = "smtp-mail.outlook.com";
    
        Properties props = System.getProperties();
        props.put("mail." + prot + ".host", mailhost);
        props.put("mail." + prot + ".auth", "true");
        props.put("mail." + prot + ".starttls.enable", "true");
        props.put("mail." + prot + ".port", "587");
        props.put("mail." + prot + ".ssl.enable", "false");
    
        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, pass);
                    }
                });
        session.setDebug(true);
    
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(user));
        msg.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to, false));
    
        msg.setSubject("TESTE ENVIO EMAIL");
    
        msg.setText("Teste de envio de email conteudo");
    
        msg.setHeader("X-Mailer", user);
        msg.setSentDate(new Date());
    
        SMTPTransport t =
                (SMTPTransport) session.getTransport(prot);
    
        t.connect(mailhost, user, pass);
    
        t.sendMessage(msg, msg.getAllRecipients());
        t.close();
    
        Log.d("envio de email", "enviado");
    } catch (Exception e) {
        Log.e("Erro", "actionbt1", e);
    }
    

【讨论】:

    【解决方案4】:

    我的应用程序在通过网络数据连接时无法通过 Gmail 发送电子邮件,但它在 WiFi 上运行良好。

    Google 因可疑而拒绝了新 IP 地址。

    选项 1:

    要清除验证码,请在登录到您的 Google 帐户时访问此链接:https://accounts.google.com/DisplayUnlockCaptcha

    选项 2:

    在您的 Google 帐户上启用两步验证。然后使用以下链接生成应用程序特定密码 (ASP)。用 ASP 替换代码中常用的 gmail 密码。

    https://accounts.google.com/IssuedAuthSubTokens?hide_authsub=1

    (在第一个下拉列表中选择“邮件”选项,然后在第二个下拉列表中使用“其他”选项插入您的应用名称或您喜欢的任何其他名称)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多