gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:05:54 26 4
gpt4 key购买 nike

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

public static void main(String[] args) {

final String username = "emailId@gmail.com";
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("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("email1@google.com"));
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

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

但是得到异常

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)

最佳答案

尝试改变:

<groupId>javax.mail</groupId>

到:

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

关于java - 如何修复 ClassNotFoundException : com. sun.mail.util.MailLogger?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45311884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com