I am trying to send mails to users when they successfully register their account i.e email verification/confirmation. However, I keep getting the errors above.
This is what the MailCreatorHelper looks like
@Service
@Slf4j
public class MailCreatorHelper {
@Autowired
JavaMailSender javaMailSender;
public static String getSiteURL(HttpServletRequest request){
String siteURL = request.getRequestURL().toString();
return siteURL.replace(request.getServletPath(), "");
}
private String setVerificationCodeForUser(){
log.info("Generating authentication code --------------------------------");
return RandomString.make(35);
}
private void setProperties(){
Properties props = new Properties();
props.setProperty("mail.host", "smtp.mailgun.org");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable", "true");
props.setProperty("mail.smtp.socketFactory.port", "587");
props.setProperty("mail.smtp.socketFactory.class", "");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
Authenticator auth = new SMTPAuthenticator("username", "password");
Session session = Session.getInstance(props, auth);
}
public MimeMessage createMessage(User user, String siteURL) throws MessagingException {
setProperties();
String mailSubject = "Activate your account";
String sender = "valid@email.com";
String template = "Dear [[name]],<br>"
+ " Thanks for registering on xxxxxxx"
+ " Kindly click the link below to verify your email and activate your account"
+ " <h4><a href=\"[[URL]]\" target=\"_self\"> Verify email </a></h3>"
+ "Thank you."
+ " xxxx team";
final Context context = new Context();
MimeMessage message = this.javaMailSender.createMimeMessage();
// MimeMessageHelper mimeMessage = new MimeMessageHelper(message, true, "UTF-8");
MimeMessageHelper mimeMessage = new MimeMessageHelper(message);
mimeMessage.setFrom(sender);
mimeMessage.setSubject(mailSubject);
// mimeMessage.setReplyTo(sender);
mimeMessage.setTo(user.getEmail());
// String htmlContent = this.templateEngine.process(template, context);
template = template.replace("[[name]]", user.getUsername());
String verifyURL = siteURL + "/user/verify/" + user.getVerificationCode();
template = template.replace("[[URL]]", verifyURL);
// mimeMessage.setText(template, true);
mimeMessage.setText(template);
log.info(String.valueOf(message));
return message;
}
public MimeMessage createConfirmationEmail(User user, String siteURL) throws UnsupportedEncodingException, MessagingException {
String toAddress = user.getUsername();
String fromAddress = "valid@email.com";
String senderName = "Hello, World!";
String subject = "Welcome to xxxxx";
String content = "Dear [[name]]," +
"<br><br>"
+ "Your account have been verified, kindly login to explore<br>"
+ "<h3><a href=\"[[URL]]\" target=\"_self\">Login</a></h3> <br>"
+ "Thank you," +
"<br><br>"
+ "xxxxx Team";
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom(fromAddress, senderName);
helper.setTo(toAddress);
helper.setSubject(subject);
content = content.replace("[[name]]", user.getUsername());
String verifyURL = siteURL + "/login" ;
content = content.replace("[[URL]]", verifyURL);
helper.setText(content, true);
return message;
}
public MimeMessage createResetPasswordMessage(User user, String siteURL) throws MessagingException {
setProperties();
String mailSubject = "Reset your password";
String sender = "smartsammie@gmail.com";
String template = "Dear [[name]],<br>"
+ " Kindly click the link below to reset your password"
+ " Ignore this email if you did not make this request"
+ " <h4><a href=\"[[URL]]\" target=\"_self\"> Reset password </a></h3>"
+ "Thank you."
+ " Stay healthy"
+ "xxxxx team";
final Context context = new Context();
MimeMessage message = this.javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessage = new MimeMessageHelper(message);
mimeMessage.setFrom(sender);
mimeMessage.setSubject(mailSubject);
mimeMessage.setTo(user.getEmail());
// String htmlContent = this.templateEngine.process(template, context);
template = template.replace("[[name]]", user.getUsername());
String resetPasswordURL = siteURL + "/user/reset-password/" + user.getVerificationCode();
template = template.replace("[[URL]]", resetPasswordURL);
// mimeMessage.setText(template, true);
mimeMessage.setText(template);
log.info(String.valueOf(message));
return message;
}
private class SMTPAuthenticator extends Authenticator
{
private PasswordAuthentication authentication;
public SMTPAuthenticator(String login, String password)
{
authentication = new PasswordAuthentication(login, password);
}
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return authentication;
}
}
}
What could be wrong? I am not sending any mail in the controller rather I am doing it in the service implementation. I have gone through some couple of threads and nothing seems to work for me
Here is the complete stack trace
2021-11-05 18:54:35.547 ERROR 17082 --- [nio-8080-exec-2] 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: java.lang.NullPointerException: Cannot invoke "javax.mail.internet.MimeMessage.getSentDate()" because "mimeMessage" is null; message exceptions (1) are:
Failed message 1: java.lang.NullPointerException: Cannot invoke "javax.mail.internet.MimeMessage.getSentDate()" because "mimeMessage" is null] with root cause
org.springframework.mail.MailSendException: Failed messages: java.lang.NullPointerException: Cannot invoke "javax.mail.internet.MimeMessage.getSentDate()" because "mimeMessage" is null
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:491) ~[spring-context-support-5.3.10.jar:5.3.10]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361) ~[spring-context-support-5.3.10.jar:5.3.10]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:356) ~[spring-context-support-5.3.10.jar:5.3.10]
at com.etranscript.eTranscriptBackendV2.emailService.services.EmailServiceImpl.sendActivateAccountMessage(EmailServiceImpl.java:32) ~[classes/:na]
at com.etranscript.eTranscriptBackendV2.service.services.ApplicantServiceImpl.register(ApplicantServiceImpl.java:82) ~[classes/:na]
at com.etranscript.eTranscriptBackendV2.web.api.ApplicantController.registerUser(ApplicantController.java:49) ~[classes/:na]
at com.etranscript.eTranscriptBackendV2.web.api.ApplicantController$$FastClassBySpringCGLIB$$65c3e4b7.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.10.jar:5.3.10]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.3.10.jar:5.3.10]
at com.etranscript.eTranscriptBackendV2.web.api.ApplicantController$$EnhancerBySpringCGLIB$$17e326c1.registerUser(<generated>) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[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:564) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.10.jar:5.3.10]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.10.jar:5.3.10]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.10.jar:5.3.10]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) ~[tomcat-embed-core-9.0.53.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.10.jar:5.3.10]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.53.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.53.jar:9.0.53]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at com.etranscript.eTranscriptBackendV2.securityService.security.JwtAuthenticationFilter.doFilterInternal(JwtAuthenticationFilter.java:76) ~[classes/:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.10.jar:5.3.10]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at com.etranscript.eTranscriptBackendV2.securityService.security.CORSConfig.doFilter(CORSConfig.java:48) ~[classes/:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:204) ~[spring-security-web-5.5.2.jar:5.5.2]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:183) ~[spring-security-web-5.5.2.jar:5.5.2]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358) ~[spring-web-5.3.10.jar:5.3.10]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271) ~[spring-web-5.3.10.jar:5.3.10]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.10.jar:5.3.10]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.10.jar:5.3.10]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.10.jar:5.3.10]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.10.jar:5.3.10]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.10.jar:5.3.10]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.10.jar:5.3.10]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1726) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.53.jar:9.0.53]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]