What is wrong in my code for get error " org.springframework.mail.MailSendException"?

时间:2019-05-31 11:39:09

标签: java smtp gmail javabeans mail-sender

what i use bad in my code for i get this error:

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
  nested exception is:
    java.net.ConnectException: Connection refused: connect. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
  nested exception is:
    java.net.ConnectException: Connection refused: connect; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
  nested exception is:
    java.net.ConnectException: Connection refused: connect

I use ApplicationConfiguration where i define this Bean:

@Bean(name = "javaMailSender")
    public JavaMailSender javaMailSender(){
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
        props.put("mail.smtp.username","myAddress@gmail.com");
        props.put("mail.smtp.password","myPass");

        Session session = Session.getInstance(props,
                                              new javax.mail.Authenticator()
                                              {
                                                  protected PasswordAuthentication getPasswordAuthentication()
                                                  {
                                                      return new PasswordAuthentication("myAddress@gmail.com", "myPass");
                                                  }
                                              });
        mailSender.setSession(session);
        mailSender.setPort(587);
        mailSender.setHost("smtp.gmail.com");
        mailSender.setProtocol("smtp");

        mailSender.setUsername("myAddress@gmail.com");
        mailSender.setPassword("myPass");
        return mailSender;
    }

I use just this code for config bean. Does anyone have an idea to solve it?

I try telnet smtp.gmail.com 587 and i get

"220 smtp.gmail.com ESMTP 6sm10280881wrd.51 - gsmtp"

0 个答案:

没有答案