简体   繁体   中英

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("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;
    }
}

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

问题已修复,我犯了在 Modulepath 而不是 Classpath 下导入外部 .jar 的愚蠢错误。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM