java.lang.RuntimeException:javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:465

java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465

大家好Whosebug!

我一直在尝试使用 JavaMail API

// Recipient's email ID needs to be mentioned.
          String to = "toaddrs@gmail.com";//change accordingly

          // Sender's email ID needs to be mentioned
          String from = "fromaddrs@gmail.com";//change accordingly
          final String email = "emailaddrs@gmail.com";//change accordingly
          final String password = "xxxxxxxxx";//change accordingly

          // Assuming you are sending email through relay.jangosmtp.net
          String host = "smtp.gmail.com";

          Properties props = new Properties();
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.port", "465");

          // Get the Session object.
          Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
             protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(email, password);
             }
          });

          try {
             // Create a default MimeMessage object.
             Message message = new MimeMessage(session);

             // Set From: header field of the header.
             message.setFrom(new InternetAddress(from));

             // Set To: header field of the header.
             message.setRecipients(Message.RecipientType.TO,
             InternetAddress.parse(to));

             // Set Subject: header field
             message.setSubject("Testing Subject");

             // Now set the actual message
             message.setText("Hello, this is sample for to check send "
                + "email using JavaMailAPI ");

             // Send message
             Transport.send(message);

             System.out.println("Sent message successfully....");

          } catch (MessagingException e) {
                throw new RuntimeException(e);
          }

程序似乎没有错误,执行后我得到:

java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; nested exception is: java.net.ConnectException: Connection timed out: connect

有人可以帮我解决这个问题吗??? << -> 是的,我已经配置了 GMail 访问安全性较低的应用程序的选项.... >>

提前致谢!!

当使用 TLS 通过 Gmail SMTP 服务器发送邮件时,您应该使用端口 587

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");

端口 465 用于使用 SSL 发送电子邮件,应该看起来像这样

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

您的防火墙或代理或防病毒程序可能正在阻止直接连接。

JavaMail 常见问题解答有 tips for connecting through a proxy, and debugging tips to help determine why you can't connect