我试图用java发送电子邮件。 这是代码:
String mailSmtpHost = "smtp.example.com";
String mailTo = "email@example.com";
String mailFrom = "email@example.com";
String mailSubject = "Email subject";
String mailText = "Some text";
Properties properties = new Properties();
properties.put("mail.smtp.host", mailSmtpHost);
Session emailSession = Session.getDefaultInstance(properties);
try {
Message emailMessage = new MimeMessage(emailSession);
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
emailMessage.setFrom(new InternetAddress(mailFrom));
emailMessage.setSubject(mailSubject);
emailMessage.setText(mailText);
emailSession.setDebug(true);
Transport.send(emailMessage);
}catch(Exception e) {
System.out.println("Errore email: "+e.toString());
}
当我编译项目时,在线:
Message emailMessage = new MimeMessage(emailSession);
......出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataHandler
注意:我使用java -version: 9.0.4
答案 0 :(得分:12)
JDK 9默认禁用对许多javax。* API的访问,现在不推荐使用javax激活。
但是,您可以通过在运行时添加模块来解决此问题
"--add-modules java.activation".
答案 1 :(得分:0)
下载并添加外部jakarta.activation-1.2.1.jar 是Jakarta Activation项目。