如何解决此问题“无法连接到主机,端口:smtp.gmail.com,587;超时-1”

时间:2019-04-11 04:22:33

标签: java spring-boot javamail

尝试通过Spring Boot发送电子邮件时出现以下错误消息:

路径为[]的上下文中的Servlet [dispatcherServlet]的Servlet.service()抛出异常[请求处理失败;嵌套异常是com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,587;无法连接到主机。超时-1;

private void sendmail(EmailMessage emailmessage) throws AddressException, MessagingException, IOException {

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

    Session session = Session.getInstance(props,
              new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
              });

0 个答案:

没有答案