gpt4 book ai didi

Java Mail 找不到类 MimeMessage

转载 作者:行者123 更新时间:2023-11-30 02:02:04 31 4
gpt4 key购买 nike

这是Mail类的代码(里面有一个main,但原因很简单,这样解决这个问题似乎很简单):

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class Mail {

public static void main(String [] args) {
// Recipient's email ID needs to be mentioned.
String to = "abcd@gmail.com";

// Sender's email ID needs to be mentioned
String from = "mail";
String psw = "password";

// Assuming you are sending email from localhost
String host = "localhost";

// Get system properties
Properties properties = System.getProperties();

// Setup mail server
properties.setProperty("mail.smtps.host", host);
properties.setProperty("mail.user", from);
properties.setProperty("mail.password", psw);

// Get the default Session object.
Session session = Session.getDefaultInstance(properties);

try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

// Set Subject: header field
message.setSubject("This is the Subject Line!");

// Now set the actual message
message.setText("This is actual message");

// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}

这是我运行后看到的终端:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataSource
at Mail.main(Mail.java:35)
Caused by: java.lang.ClassNotFoundException: javax.activation.DataSource
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 1 more

Process finished with exit code 1

错误发生:

MimeMessage message = new MimeMessage(session);

最佳答案

您要么需要告诉 JDK 9 公开包含但隐藏的 java.activation 模块,要么需要在项目中显式包含 JavaBeans Activation Framework(JAF;javax.activation)jar 文件。

通过将 --add-modules java.activation 添加到 java 命令行来执行前者。

后者可以通过使用此 Maven 依赖项来完成:

<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>

关于Java Mail 找不到类 MimeMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52515171/

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