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

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

我正在尝试制作一个基本的应用程序,只需单击一个按钮即可向某人发送电子邮件。

我正在使用 activation.jar 和 javax.mail.jar,它们都被配置为在属性 > Java 构建路径 > 添加外部 Jars 下在 eclipse 中使用。

我的代码没有错误,一切看起来都很正常。当我尝试 运行 程序时,出现标题中提到的错误。

这是我的代码:

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("xx@xx.com");
    }

    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 = "xx@xx.com";
        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;
    }
}

我找到了很多解决我的问题的方法,但是 none 中的方法对我的情况很成功。提前致谢!

问题已解决,我犯了一个愚蠢的错误,即在 Modulepath 而不是 Classpath 下导入外部 .jar。