I want to implement forgot password functionality. My app should send an email to the employee. Method send() of my JavaMailSender object is not working, and I don't receive any exception message. I've tried a lot of things. For example:
Having troubles sending emails from spring boot application
No exception found but mail not send in Spring MVC
Java spring boot - JavaMailSender errors
My ForgotPasswordController.java class (part that has a problem):
@PostMapping("/forgot_password")
public String processForgotPassword(HttpServletRequest request, Model model)
{
String email = request.getParameter("email");
String token = RandomString.make(30);
try
{
employeeDAO.updateResetPasswordToken(token, email);
String resetPasswordLink = Utility.getSiteURL(request) + "/reset_password?token=" + token;
sendEmail(email, resetPasswordLink);
model.addAttribute("message", "We have sent a reset password link to your email. Please check.");
}
catch (Exception e)
{
model.addAttribute("error", "Error while sending email");
}
return "forgot_password_form";
}
public void sendEmail(String recipientEmail, String link) throws Exception
{
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(new InternetAddress("svemir2212@gmail.com"));
helper.setTo(recipientEmail);
String subject = "Here's the link to reset your password";
String content = "<p>Hello,</p>"
+ "<p>You have requested to reset your password.</p>"
+ "<p>Click the link below to change your password:</p>"
+ "<p><a href=\"" + link + "\">Change my password</a></p>"
+ "<br>"
+ "<p>Ignore this email if you do remember your password, "
+ "or you have not made the request.</p>";
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
}
I'm using Spring Boot. Here is my config for email in application.properties file:
spring.mail.host = smtp.gmail.com
spring.mail.port = 465
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
spring.mail.username = mymail@gmail.com
spring.mail.password = somepassword
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.auth.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.enable = true
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false
I've tried with port 587 also, doesn't work.