javaemailjakarta-mailmime-message

I'm getting NoClassDefFoundError at javax.mail.MimeMessage in intellij


I have recently downloaded javax.mail from a website https://javaee.github.io/javamail/

I'm using IntelliJ as my java IDE and I have added the jar file as it shown in YouTube videos. IntelliJ also shows no errors [I mean that the jar is added successfully].

I typed a sample program as I saw in YouTube.

The program that I typed is given below.

package com.company;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailTest {
    public static void main(String[] args) {
        final String username="username";
        final String password="password";
        final String from="from@gmail.com";
        final String to="to@gmail.com";
        Properties properties=new Properties();
        properties.put("mail.smtp.auth","true");
        properties.put("mail.smtp.starttls.enable","true");
        properties.put("mail.smtp.host","smtp.gmail.com");
        properties.put("mail.smtp.port","587");
        Session session=Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username,password);
            }
        });
        MimeMessage msg=new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(from));
            msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
            msg.setSubject("Message from JAVA");
            msg.setText("Sample body");
/*
            Multipart multipart=new MimeMultipart();
            MimeBodyPart mimeBodyPart=new MimeBodyPart();
            mimeBodyPart.setText("Please open the file attached below");
            MimeBodyPart attachment=new MimeBodyPart();
            attachment.attachFile("test.txt");
            multipart.addBodyPart(mimeBodyPart);
            multipart.addBodyPart(attachment);
            msg.setContent(multipart);
*/
            Transport.send(msg);
            System.out.println("Mail sent successfully");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

The code is same as I saw in YouTube and the IDE also shows no error. But when I run the code I expect it to run as it ran for the YouTuber's.

But I got NoClassDefFoundError at the line 30 at this line.

MimeMessage msg=new MimeMessage(session);

I have no idea what to do now? Can anyone please help me out in this...

This is the error which I'm getting below

Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataHandler
    at com.company.MailTest.main(MailTest.java:30)
Caused by: java.lang.ClassNotFoundException: javax.activation.DataHandler
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
    ... 1 more

Process finished with exit code 1



Solution

  • You have to download javax.activation jar