Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception: java.lang.ClassNotFoundException: javax.activation.DataHandler even though javax.mail.jar is under classpath?

I am doing an application that sends emails in java as a final project for my Computer science course. As many online tutorials said, I have imported everything that I can download at this page: https://javaee.github.io/javamail/#Download_JavaMail_Release and those jar files are all in my classpath. I followed exactly on the tutorial, However, for some reason, I am still getting the error. here is the code for SendEmail class:

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import javax.swing.*;

public class sendEmail {

    public sendEmail() {

    }

    protected void send(String sender, String recipient, String password, String subject, String content) {
        Properties prop = new Properties();
        prop.put("mail.stmp.auth*", "true");// set the authentication to true
        prop.put("mail.stmp.starttls.enable*", "true ");
        prop.put("mail.stmp.host*", "stmp.gamil.com");
        prop.put("mail.stmp.port*", "587");
        Session ses = Session.getInstance(prop, new javax.mail.Authenticator() {
            private PasswordAuthentication getPassWordAuthrntication() {
                return new PasswordAuthentication(sender, password);
            }
        });
        try {
            Message msg = new MimeMessage(ses);
            msg.setFrom(new InternetAddress(sender));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
            msg.setSubject(subject);
            msg.setContent(content,"text/html;charset=utf-8");
            Transport.send(msg);
            sent();

        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException m) {
            throw new RuntimeException(m);
        }
    }
    private void sent() { // a nitification saying that the email is //sucessfully sent
        JDialog d = new JDialog();
        d.setTitle("Notice");
        d.setBounds(1366 / 2 - 100, 768 / 2 - 75, 200, 150);
        JLabel label = new JLabel("Your Email has been sent!");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        d.getContentPane().add(label);
        d.setVisible(true);
    }
}

Here is what's thrown:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: javax/activation/DataHandler
    at sendEmail.send(sendEmail.java:26)
    at EditEmail.send(EditEmail.java:120)
    at EditEmail$buttonAction.actionPerformed(EditEmail.java:147)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6632)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6397)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.ClassNotFoundException: javax.activation.DataHandler
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 38 more

SendEmail is the class that is supposed to send the email, EditEmail just calls the the method in SendMail. here is my classpath:Picture of the class folder properties

enter image description here

like image 759
Cloud Walker Avatar asked Jan 10 '20 03:01

Cloud Walker


1 Answers

DataHandler comes from JAF; it's not part of JavaMail. JAF is included in JDK 1.8 and older. You must be using a newer JDK. You need com.sun.activation:javax.activation:1.2.0.

like image 184
Bill Shannon Avatar answered Nov 06 '22 23:11

Bill Shannon