简体   繁体   English

Spring 和 JavaMail 连接超时

[英]Spring and JavaMail connection timeout

I'm trying to do email confirmation via gmail host.我正在尝试通过 gmail 主机进行电子邮件确认。 And i continue getting "Connection timeout 500 error" I tried change params in app properties, and in configuration class.我继续收到“连接超时 500 错误”我尝试更改应用程序属性和配置类中的参数。 Change port to 465 insteed 587, and nothing helped me.将端口更改为 465 而不是 587,但没有任何帮助。

If you found this page unreadable, which might be... there's link on full project with full stack trace ---> https://github.com/OlegSokolyk/sweater如果您发现此页面不可读,这可能是...完整项目的链接带有完整的堆栈跟踪 ---> https://github.com/OlegSokolyk/sweater

here's my app prop:这是我的应用程序道具:

spring.mail.host=smtp.gmail.com
spring.mail.username=mailsendtestsweater@gmail.com
spring.mail.password=vhmixskurxwrymtu
spring.mail.port=587
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
mail.debug=true

MailConfig class:邮件配置类:

@Configuration
public class MailConfig {

    @Value("${spring.mail.host}")
    private String host;

    @Value("${spring.mail.username}")
    private String username;

    @Value("${spring.mail.password}")
    private String password;

    @Value("${spring.mail.port}")
    private int port;

    @Value("${mail.debug}")
    private String debug;

    @Bean
    public JavaMailSender getMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

        mailSender.setHost(host);
        mailSender.setPort(port);
        mailSender.setUsername(username);
        mailSender.setPassword(password);

        Properties properties = mailSender.getJavaMailProperties();

        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.debug", debug);

        return mailSender;
    }
}

MailSender service:邮件发送服务:

@Service
public class MailSender {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String username;


    public void send(String emailTo, String subject, String message) {

        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();

            mailMessage.setFrom(username);
            mailMessage.setTo(emailTo);
            mailMessage.setSubject(subject);
            mailMessage.setText(message);

            mailSender.send(mailMessage);
        } catch (MailException e) {
            e.printStackTrace();
        }
    }
}

Registration method:报名方式:

public boolean addUser(User user) {
        User userFromDB = userRepository.findByUsername(user.getUsername());

        if(userFromDB != null) {
            return false;
        }

        user.setActive(true);
        user.setRoles(Collections.singleton(Role.USER));
        user.setActivationCode(UUID.randomUUID().toString());

        userRepository.save(user);

        if (!StringUtils.isEmpty(user.getEmail())) {
            String message = String.format(
                    "Hello, %s! \n " +
                            "Welcome to Sweater. Please, visit next link: http://localhost:8080/activate/%s",
                    user.getUsername(),
                    user.getActivationCode()
            );

            mailSender.send(user.getEmail(), "Activation code", message);
        }

        return true;
    }

Registration controller:注册控制器:

@PostMapping("/registration")
    public String addUser(User user, Model model) {

        if (!userService.addUser(user)) {
            model.addAttribute("message", "User exists");
            return "registration";
        }

        return "redirect:/login";
    }

Stack trace:堆栈跟踪:

org.springframework.mail.MailSendException: Mail server connection failed; org.springframework.mail.MailSendException: 邮件服务器连接失败; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587;嵌套异常是 com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,587; timeout -1;超时 -1;
nested exception is: java.net.ConnectException: Connection timed out: connect.嵌套异常是:java.net.ConnectException:连接超时:连接。 Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587;失败的消息:com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,587; timeout -1;超时 -1;
nested exception is:嵌套异常是:

spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.writetimeout=5000 spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.writetimeout=5000

try adding above two properties尝试添加以上两个属性

The reason was my internet provider.原因是我的互联网提供商。 I tried the same code in antoher one and it worked!我在另一个代码中尝试了相同的代码,并且成功了!

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

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