java 如何修复 ClassNotFoundException:com.sun.mail.util.MailLogger?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45311884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:37:43  来源:igfitidea点击:

How can I fix ClassNotFoundException: com.sun.mail.util.MailLogger?

javajavamail

提问by Prince

I want to send email, My code is below

我想发送电子邮件,我的代码在下面

public static void main(String[] args) {

    final String username = "[email protected]";
    final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("[email protected]"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler,"
            + "\n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

POM.xml

POM文件

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.5.5</version>
</dependency>

But getting exception

但得到例外

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
at javax.mail.Session.initLogger(Session.java:230)
at javax.mail.Session.<init>(Session.java:214)
at javax.mail.Session.getInstance(Session.java:251)
at com.smart21.spring.utils.MailTest.main(MailTest.java:26)
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

回答by Kohei TAMURA

Try changing from:

尝试从:

<groupId>javax.mail</groupId>

to:

到:

<groupId>com.sun.mail</groupId>

回答by Hymankobec

The solution is to use correct dependency. For gradle:

解决方案是使用正确的依赖项。对于毕业:

// https://mvnrepository.com/artifact/javax.mail/mail
compile group: 'javax.mail', name: 'mail', version: '1.5.0-b01'

回答by Yoshita Mahajan

Try taking activation.jarand mail.jarfrom the Oracle website. This would help you resolve the error ClassNotFoundException: com.sun.mail.util.MailLogger.

尝试从 Oracle 网站获取activation.jarmail.jar。这将帮助您解决错误ClassNotFoundException: com.sun.mail.util.MailLogger

回答by Ashley Frieze

This is a duplicate of the issue in java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mailand the answer that worked for me:

这是java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail中的问题的副本,以及对我有用的答案:

        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>

See https://stackoverflow.com/a/16808343/1355930

https://stackoverflow.com/a/16808343/1355930