Getting Error: java.lang.NoClassDefFoundError: javax/mail/MessagingException

I’m trying to make a basic application that sends an email to someone with the click of a button.

I’m using activation.jar and javax.mail.jar, both of which are configured to be used within eclipse under properties > Java Build Path > Add External Jars.

I have no errors within my code, everything looks normal. When I attempt to run the program I get the error mentioned in the title.

Here is my code:

package me.hunter;

import java.util.Properties;

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;

public class InstaEmail {

    public static void main(String args[]) throws Exception {
        email("[email protected]");
    }

    public static void email(String recepient) throws Exception {
        System.out.println("Email sending...");
        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.auth", "587");

        String email = "[email protected]";
        String password = "xxxxxxx";


        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(email, password);
            }
        });

        Message message = prepareMessage(session, email, recepient);

        try {
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        System.out.println("Message sent!");
    }

    private static Message prepareMessage(Session session, String email, String recepient) {

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(email));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
            message.setSubject("Need Help Promoting X!");
            message.setText("Hey there, n I want stickers!");

        } catch(Exception ex) {
            System.out.println(ex);
        }

        return null;
    }
}

I have found numerous solutions to my problem, but none of them have shown any success in my case. Thanks in advance!

Answer

Issue fixed, I made the dumb mistake of importing the external .jar under Modulepath instead of Classpath.

Leave a Reply

Your email address will not be published. Required fields are marked *