【问题标题】:Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1无法连接到 SMTP 主机:smtp.gmail.com,端口:465,响应:-1
【发布时间】:2013-02-28 23:08:06
【问题描述】:

发送邮件时出现此错误

java.lang.RuntimeException: javax.mail.SendFailedException: 发送 失败的;嵌套异常是:类 javax.mail.MessagingException: 无法连接到 SMTP 主机:smtp.gmail.com,端口:465,响应: -1

我的代码是:

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", "465");
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("email","password");
                }
        });

try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("email"));
        message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(this.to));
        message.setSubject("Testing");
        message.setText("Hey, this is the testing email.");



        Transport.send(message);

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

  • 我尝试了相同的代码,但仍然出现错误:javax.mail.MessagingException: 530 5.7.0 必须先发出 STARTTLS 命令。 4sm28216799pbn.23 - gsmtp 在 com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020) 在 com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716) 在 com.sun.mail。 smtp.SMTPTransport.sendMessage(SMTPTransport.java:388) at rapid.mail.main(mail.java:60) starttls 的这一行是为什么我得到同样的错误:properties.put("mail.smtp.starttls.enable ", "真");
  • 试试最新的jar版本JavaMail 1.4.7
  • 没有进展同样的错误
  • 将端口号改为587。

标签: java smtp gmail


【解决方案1】:

就我而言,是 Avast Antivirus 干扰了连接。 禁用此功能的操作: Avast -> 设置-> 组件 -> Mail Shield(自定义)-> SSL 扫描 -> 取消选中“扫描 SSL 连接”。

【讨论】:

    【解决方案2】:

    你需要告诉它你正在使用 SSL:

    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    

    如果你错过了什么,这里是工作代码:

    String  d_email = "address@gmail.com",
                d_uname = "Name",
                d_password = "urpassword",
                d_host = "smtp.gmail.com",
                d_port  = "465",
                m_to = "toAddress@gmail.com",
                m_subject = "Indoors Readable File: " + params[0].getName(),
                m_text = "This message is from Indoor Positioning App. Required file(s) are attached.";
        Properties props = new Properties();
        props.put("mail.smtp.user", d_email);
        props.put("mail.smtp.host", d_host);
        props.put("mail.smtp.port", d_port);
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.debug", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", d_port);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
    
        SMTPAuthenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(props, auth);
        session.setDebug(true);
    
        MimeMessage msg = new MimeMessage(session);
        try {
            msg.setSubject(m_subject);
            msg.setFrom(new InternetAddress(d_email));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
    
    Transport transport = session.getTransport("smtps");
                transport.connect(d_host, Integer.valueOf(d_port), d_uname, d_password);
                transport.sendMessage(msg, msg.getAllRecipients());
                transport.close();
    
            } catch (AddressException e) {
                e.printStackTrace();
                return false;
            } catch (MessagingException e) {
                e.printStackTrace();
                return false;
            }
    

    【讨论】:

    • 我遇到了异常。你能告诉我哪里失败了 javax.mail.SendFailedException: Sending failed;嵌套异常是:类 javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587;嵌套异常是:java.net.ConnectException:连接超时:在 javax.mail.Transport.send(Transport.java:80) 在 sendMail.SendMail4 处连接 javax.mail.Transport.send0(Transport.java:218)。在 sendMail.sel.main(sel.java:10) 处执行(SendMail4.java:70)
    • 谢谢。我不知道d_uname 字段实用程序是什么。另一方面,在连接指令中,我们应该传递电子邮件而不是用户名:transport.connect(d_host, Integer.valueOf(d_port), d_email, d_password);。此外,您最好在代码中显示类 SMTPAuthenticator 的来源包,或者在答案中指定所需的 API。
    【解决方案3】:

    我所做的是我注释掉了

    props.put("mail.smtp.starttls.enable","true"); 
    

    因为显然对于 G-mail,您不需要它。然后,如果您还没有这样做,您需要在 G-mail 中为您的程序创建一个应用程序密码。我做到了,而且效果很好。在这里,此链接将向您展示如何操作:https://support.google.com/accounts/answer/185833。

    【讨论】:

      【解决方案4】:

      我在使用 NetBeans 进行调试时遇到了这个问题,甚至执行了实际的 jar 文件。防病毒软件会阻止发送电子邮件。您应该在调试期间暂时禁用防病毒软件,或者将 NetBeans 和实际的 jar 文件排除在扫描范围之外。就我而言,我使用的是 Avast。

      查看此链接了解如何排除:How to Add File/Website Exception into avast! Antivirus 2014

      它对我有用。

      【讨论】:

      • 您能否更详细地描述一下 SMTP 响应和防病毒之间的联系?
      • @carlodurso:防病毒软件(在我的例子中是 AVAST)具有阻止使用端口进出怀疑安全威胁的程序的功能。所以最好排除你的 jar 文件被阻止。
      【解决方案5】:

      端口 465 用于“smtp over SSL”。

      http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html

      [...] For example, use
          props.put("mail.smtp.port", "888");
      to set the mail.smtp.port property, which is of type int.
      
      Note that if you're using the "smtps" protocol to access SMTP over SSL, 
      all the properties would be named "mail.smtps.*"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-10
        • 1970-01-01
        • 2014-03-08
        • 2015-12-27
        • 1970-01-01
        • 1970-01-01
        • 2012-01-25
        • 2011-12-09
        相关资源
        最近更新 更多