Java Mail找不到类MimeMessage [英] Java Mail can't find the class MimeMessage
问题描述
这是Mail类的代码(内部有一个主程序,但出于简单的原因,以这种方式解决该问题似乎很简单):
This is the code of the class Mail (there are a main inside but for the simple reason that in this way it seem to be simple to solve this problem):
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();
}
}
}
这是我跑步后看到的终点站:
And this is the terminal i see after the running:
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消息=新的MimeMessage(会话);
MimeMessage message = new MimeMessage(session);
推荐答案
您要么需要告诉JDK 9公开包含但隐藏的java.activation模块,要么需要包括JavaBeans Activation Framework(JAF; javax .activation)项目中的jar文件.
You either need to tell JDK 9 to expose the included-but-hidden java.activation module, or you need to include the JavaBeans Activation Framework (JAF; javax.activation) jar file in your project explicitly.
通过在java
命令行中添加--add-modules java.activation
来执行前者.
Do the former by adding --add-modules java.activation
to your java
command line.
后者可以通过使用以下Maven依赖项来完成:
The latter can be done by using this Maven dependency:
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
这篇关于Java Mail找不到类MimeMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!