得到错误:java.lang.NoClassDefFoundError:javax / mail / MessagingException [已修复]

问题描述 投票:0回答:2

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

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

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

这是我的代码:

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

我已经找到了解决我问题的众多解决方案,但是在我的案例中,没有一个显示出任何成功。预先感谢!

java javamail
2个回答
0
投票

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


-1
投票

尝试使用Javax邮件1.4.7

https://mvnrepository.com/artifact/javax.mail/mail/1.4.7

它将解决无类定义问题。

如果还有其他问题,请发表评论。

© www.soinside.com 2019 - 2024. All rights reserved.