Не работает MailSender
MailSender.java
@Service
public class MailSender {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String username;
public void send(String emailTo, String subject, String message){
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(username);
mailMessage.setTo(emailTo);
mailMessage.setSubject(subject);
mailMessage.setText(message);
mailSender.send(mailMessage);
}
}
Mailconfig.java
@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("${spring.mail.protocol}")
private String protocol;
@Value("${mail.debug}")
private String debug;
@Value("${spring.mail.properties.mail.smtp.auth}")
private String auth;
@Value("${spring.mail.properties.mail.smtp.starttls.enable}")
private String enable;
@Bean
public JavaMailSender getEmailSender(){
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setUsername(username);
mailSender.setPassword(password);
Properties properties = mailSender.getJavaMailProperties();
properties.setProperty("mail.transport.protocol", protocol);
properties.setProperty("mail.debug", debug);
properties.setProperty("mail.smtp.auth", auth);
properties.setProperty("mail.smtp.starttls.enable", enable);
return mailSender;
}
}
application.properties
spring.mail.host=smtp.gmail.com
spring.mail.username=mymail@gmail.com
spring.mail.password=mypass
spring.mail.port=587
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
mail.debug=true
Ошибка
STARTTLS
220 2.0.0 Ready to start TLS
2021-06-14 11:11:34.425 ERROR 8164 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate). Failed messages: javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate); message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)] with root cause
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)