gab*_*onc 8 java eclipse email
我正在尝试用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());
}
Run Code Online (Sandbox Code Playgroud)
当我编译项目时,在线:
Message emailMessage = new MimeMessage(emailSession);
Run Code Online (Sandbox Code Playgroud)
...出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataHandler
Run Code Online (Sandbox Code Playgroud)
注意:我正在使用 java -version: 9.0.4
Léo*_* R. 14
JDK 9默认禁用对许多javax.*API的访问,现在不推荐使用javax激活.
但是,您可以在运行时添加模块来解决它
"--add-modules java.activation".
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10416 次 |
| 最近记录: |