0

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]
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sandip Gadade
  • 199
  • 1
  • 2
  • 6
  • 1
    Does this answer your question? [Exception: java.lang.ClassNotFoundException: javax.activation.DataHandler even though javax.mail.jar is under classpath?](https://stackoverflow.com/questions/59675176/exception-java-lang-classnotfoundexception-javax-activation-datahandler-even-t) – Chaosfire Aug 15 '22 at 07:33
  • You seem to have a JakartaMail 2.0.x on your class path instead of JakartaMail 1.6.x. Please **downgrade** JakartaMail, Spring Boot 2.x is still Jakarta EE 8, so it only supports Jakarta EE libraries that still use the `javax.*` namespace. – Mark Rotteveel Aug 15 '22 at 07:48

0 Answers0