我正在尝试用Java发送电子邮件。
这是代码:
String mailSmtpHost = "smtp.example.com";
String mailTo = "[email protected]";
String mailFrom = "[email protected]";
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
最佳答案
JDK 9默认禁用对许多javax。* API的访问,现已弃用javax激活。
但是,您可以通过在运行时添加模块来解决它
"--add-modules java.activation".