I'm trying to implement Mail Sending functionality (For now I'm using some fake smtp) but I keep getting this error:
2020-09-19 00:20:38.209 DEBUG 880 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Failed to complete request: org.springframework.mail.MailSendException: Failed messages: javax.mail.SendFailedException: No recipient addresses; message exceptions (1) are:
Failed message 1: javax.mail.SendFailedException: No recipient addresses
2020-09-19 00:20:38.217 ERROR 880 --- [nio-8080-exec-5] 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: Failed messages: javax.mail.SendFailedException: No recipient addresses; message exceptions (1) are:
Failed message 1: javax.mail.SendFailedException: No recipient addresses] with root cause
org.springframework.mail.MailSendException: Failed messages: javax.mail.SendFailedException: No recipient addresses
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:491) ~[spring-context-support-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361) ~[spring-context-support-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:378) ~[spring-context-support-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:366) ~[spring-context-support-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at com.socialnetwork.service.EmailService.sendEmail(EmailService.java:26) ~[classes/:na]
at com.socialnetwork.service.EmailService.sendVerificationEmail(EmailService.java:49) ~[classes/:na]
at com.socialnetwork.controllers.AuthController.register(AuthController.java:50) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
My code:
application.properties:
mail.enable=true
mail.smtp.host=smtp.mailtrap.io
mail.smtp.port=2525
mail.smtp.user=2be3af3d5134be
mail.smtp.pass=2c9ac24fa0808e
MailConfig.java
@Configuration
public class MailConfig {
@Value("${mail.smtp.host}")
private String host;
@Value("${mail.smtp.port}")
private Integer port;
@Value("${mail.smtp.user}")
private String username;
@Value("${mail.smtp.pass}")
private String password;
@Bean
public JavaMailSender mailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setUsername(username);
mailSender.setPassword(password);
return mailSender;
}
}
And everything starts here:
@RequestMapping(value = "/register", method=RequestMethod.POST)
ModelAndView register(ModelAndView modelAndView, @ModelAttribute(value="user") @Valid SiteUser user, BindingResult result) {
modelAndView.setViewName("app.register");
if(!result.hasErrors()) {
userService.register(user);
emailService.sendVerificationEmail(user.getEmail()); // STEP 1
modelAndView.setViewName("redirect:/");
}
return modelAndView;
}
EmailService.java
@Service
public class EmailService {
@Autowired
private JavaMailSender mailSender;
@Value("${mail.enable}")
private Boolean isMailEnabled;
private void sendEmail(MimeMessagePreparator preparator) {
if(isMailEnabled) {
mailSender.send(preparator); // STEP 2
}
}
public void sendVerificationEmail(String emailAddress) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setReplyTo(emailAddress);
message.setFrom(new InternetAddress("socialnetworkmailsender@yandex.com"));
message.setSubject("Please Verify Your Email Address");
message.setSentDate(new Date());
message.setText("Hello there!");
}
};
sendEmail(preparator);
}
}
These are my classes, I left 2 comments in this code:
STEP 1: I want to send verification email after user finish registration. I'm taking email he entered and I'm passing that address to 'sendVerificationEmail' method.
STEP 2: Up to this point everything seems okay but after this call error appears on my console and I don't understand why.
NOTE: Besides mailtrap.io I tried using mailspons as well but I'm getting same error.
Do you know why I'm getting this error?
I managed to fix the error.
Problem was within 'sendVerificationEmail' method.
Instead of using:
message.setReplyTo(emailAddress);
I needed to use:
message.setTo(emailAddress);