Spring 和 JavaMail 连接超时
Spring and JavaMail connection timeout
我正在尝试通过 gmail 主机进行电子邮件确认。我继续 "Connection timeout 500 error"
我尝试在应用程序属性和配置 class 中更改参数。将端口更改为 465 insteed 587,但没有任何帮助。
如果您发现此页面不可读,这可能是......完整项目上有 link 和完整堆栈跟踪 ---> https://github.com/OlegSokolyk/sweater
这是我的应用道具:
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
邮件配置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
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();
}
}
}
报名方式:
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;
}
注册控制器:
@PostMapping("/registration")
public String addUser(User user, Model model) {
if (!userService.addUser(user)) {
model.addAttribute("message", "User exists");
return "registration";
}
return "redirect:/login";
}
堆栈跟踪:
org.springframework.mail.MailSendException: Mail server connection
failed; nested exception is com.sun.mail.util.MailConnectException:
Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is: java.net.ConnectException: Connection timed out:
connect. Failed messages: com.sun.mail.util.MailConnectException:
Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000
尝试添加以上两个属性
原因是我的互联网提供商。我在另一个代码中尝试了相同的代码,它成功了!
我正在尝试通过 gmail 主机进行电子邮件确认。我继续 "Connection timeout 500 error" 我尝试在应用程序属性和配置 class 中更改参数。将端口更改为 465 insteed 587,但没有任何帮助。
如果您发现此页面不可读,这可能是......完整项目上有 link 和完整堆栈跟踪 ---> https://github.com/OlegSokolyk/sweater
这是我的应用道具:
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
邮件配置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
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();
}
}
}
报名方式:
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;
}
注册控制器:
@PostMapping("/registration")
public String addUser(User user, Model model) {
if (!userService.addUser(user)) {
model.addAttribute("message", "User exists");
return "registration";
}
return "redirect:/login";
}
堆栈跟踪:
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is: java.net.ConnectException: Connection timed out: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.writetimeout=5000
尝试添加以上两个属性
原因是我的互联网提供商。我在另一个代码中尝试了相同的代码,它成功了!