Need to send a mail from gmail using spring boot
But i'm getting an error like
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Internal Server Error, status=500). Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout 5000; nested exception is: java.net.SocketTimeoutException: connect timed out. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout 5000; nested exception is: java.net.SocketTimeoutException: connect timed out
application.properties
spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
spring.mail.password=********
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000
Controller
@Autowired
private JavaMailSender sender;
@RequestMapping(value="/sendMail/{mail}")
public String sendMail(@PathVariable String mail) {
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
try {
helper.setTo(mail);
helper.setText("Greetings :)");
helper.setSubject("Mail From Spring Boot");
} catch (MessagingException e) {
e.printStackTrace();
return "Error while sending mail ..";
}
sender.send(message);
return "Mail Sent Success!";
}
Also allowed less secured apps in mail setting