繁体   English   中英

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

大家好!

我一直在尝试使用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:无法连接到SMTP主机:smtp.gmail.com,端口:465; 嵌套的异常是:java.net.ConnectException:连接超时:connect

有人可以帮我吗??? <<->是的,我已经配置了对不太安全的应用程序的GMail访问权限。...>>

提前Thnks!

使用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 FAQ 提供了通过代理连接的技巧调试技巧,以帮助确定为什么无法连接

暂无
暂无

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

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