I am doing a project for interview. I am working with Spring Boot and mail API. When I try to run the application, it throws class not found exception (java.lang.ClassNotFoundException: jakarta.activation.DataHandler
).
How can I handle this exception
This is my code
package com.student.service;
import java.util.Properties;
import org.springframework.stereotype.Service;
import jakarta.mail.*;
import jakarta.mail.Authenticator;
import jakarta.mail.Message;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
@Service
public class EmailService {
public boolean sendEmail(String subject, String message, String to) {
boolean f = false;
String from = "sandip@gmail.com";
String host = "smtp.gmail.com";
Properties properties = System.getProperties();
System.out.println("Properties: " + properties);
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port","465");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.auth", "true");
//step 1: to get the session object
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("sandip@gmail.com", "123");
}
});
session.setDebug(true);
//step 2: compose the message [text, multi media]
MimeMessage m = new MimeMessage(session);
try {
//from email
m.setFrom(from);
//adding recipient to message
m.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//adding subject to message
m.setSubject(subject);
//adding text to message
m.setText(message);
//step 3: send the message using Transport class
Transport.send(m);
System.out.println("Sent Success..............");
f = true;
} catch (Exception e) {
e.printStackTrace();
}
return f;
}
}
This is the error
java.lang.ClassNotFoundException: jakarta.activation.DataHandler
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]