I am trying to use JavaMailSenderImpl to send mail. Below is the program and output showing no error, but still not sending mail.
Mail Configuration:
@Configuration
public class MailConfig {
@Autowired
private Environment env;
@Bean
public JavaMailSender javaMailService() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(env.getProperty("spring.mail.host"));
mailSender.setUsername(env.getProperty("spring.mail.username"));
mailSender.setPassword(env.getProperty("spring.mail.password"));
Properties javaMailProperties = new Properties();
javaMailProperties.put("mail.smtp.ssl.enable", "true");
javaMailProperties.put("mail.smtp.starttls.enable", "false");
javaMailProperties.put("mail.smtp.auth", "true");
javaMailProperties.put("mail.transport.protocol", "smtp");
javaMailProperties.put("mail.debug", "true");
javaMailProperties.put("mail.smtp.ssl.checkserveridentity", "true");
javaMailProperties.put("mail.smtp.socketFactory.port", "465");
mailSender.setJavaMailProperties(javaMailProperties);
return mailSender;
}
}
@GetMapping("/user/resetPassword/{email}")
public GenericResponse resetPassword(HttpServletRequest request,
@PathVariable("email") String userEmail) throws UserNotFoundException {
User user = userRepository.findByEmail(userEmail);
if (user == null) {
throw new UserNotFoundException("USER NOT FOUND");
}
try {
String token = UUID.randomUUID().toString();
createPasswordResetTokenForUser(user, token);
mailSender.send(constructResetTokenEmail("http://localhost:7005", token, user));
}
catch(Exception ex) {
ex.printStackTrace();
}
return new GenericResponse("message.resetPasswordEmail");
}
public void createPasswordResetTokenForUser(User user, String token) {
PasswordResetToken myToken = new PasswordResetToken(token, user);
passwordTokenRepository.save(myToken);
}
private SimpleMailMessage constructResetTokenEmail(
String contextPath, String token, User user) {
String url = contextPath + "/user/changePassword?token=" + token;
String message = "message.resetPassword";
return constructEmail("Reset Password", message + " \r\n" + url, user);
}
private SimpleMailMessage constructEmail(String subject, String body,
User user) {
SimpleMailMessage email = new SimpleMailMessage();
email.setSubject(subject);
email.setText(body);
email.setTo("[email protected]");
email.setFrom("[email protected]");
return email;
}
}
Here is the output of the code:
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 2.1.0 OK u5sm8797330pfu.198 - gsmtp
RCPT TO:<[email protected]>
250 2.1.5 OK u5sm8797330pfu.198 - gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP: [email protected]
DATA
354 Go ahead u5sm8797330pfu.198 - gsmtp
QUIT
Experiencing the same issue I tried to find out what happened to fix the issue.
While dialing with SMTP server, after RCPT TO command, javax.mail send DATA. The server answers 354 + custom message.
It seems that on some platform (linux was the only one impacted on the software I develop) with some specific messages (the failing one had double quotes inside) the javax.mail lib just send QUIT.
As the server is waiting for the mail followed by :
.
the server just waits for other content and never answers. javax.mail fails with a timeout.
This was a dependency convergence issue :
We needed 1.6.2 due to some feature in embeds.
The solution was to exclude transitive dependency and explicitely include com.sun.mail:javax.mail at version 1.6.2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With