I'm trying to implement sending of mail template using Spring boot, but i'm constantly getting errors on jakarta's mail dependency.
Here's all of my code samples:
Email Config
@Configuration
public class EmailConfig {
@Value("${spring.mail.host}")
private String host;
@Value("${spring.mail.port}")
private int port;
@Value("${spring.mail.username}")
private String username;
@Value("${spring.mail.password}")
private String password;
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost(host);
javaMailSender.setPort(port);
javaMailSender.setUsername(username);
javaMailSender.setPassword(password);
Properties props = javaMailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
return javaMailSender;
}
}
Email Service
@Service
public class EmailServiceImpl implements EmailSender{
@Autowired
private JavaMailSender mailSender;
public void send(String to, String subject) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message, true, "UTF-8");
helper.setFrom("[email protected]");
helper.setTo(to);
helper.setSubject(subject);
helper.setText(subject, true);
mailSender.send(message);
} catch (MessagingException e) {
throw new CustomRequestException(e.getMessage(),
globaldefine.ERROR, HttpStatus.BAD_REQUEST);
}
}
}
My Email Template Config
public static String mailTemplateHelper(String title, String name, String otp){
try {
// Read the contents of index.html from the classpath
String filePath = "static/index.html";
InputStream inputStream = EmailTemplate.class.getClassLoader().getResourceAsStream(filePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
String htmlContent = stringBuilder.toString();
// Replace the placeholders with dynamic values
htmlContent = htmlContent.replace("{title}", title);
htmlContent = htmlContent.replace("{name}", name);
htmlContent = htmlContent.replace("{otp}", String.valueOf(otp));
// Return the modified HTML content
return htmlContent;
} catch (IOException e) {
e.printStackTrace();
}
// Return an empty string in case of any errors
return "";
}
public static String buildRegisterMail(String name, Integer otp) {
return mailTemplateHelper("User Verification Email Template", name, String.valueOf(otp));
} return mailTemplateHelper("User Verification Email Template", name, String.valueOf(otp));
}
And finally i let spring handle the dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Error
java.lang.IncompatibleClassChangeError: Class com.sun.mail.util.LineInputStream does not implement the requested interface jakarta.mail.util.LineInputStream
I even tried using jakarta mail api, sun mail dependecies, getting similar errors.
Any help or leads would be appreciated.