我正在用javafx创建一个聊天应用程序,并尝试使用javamail api来验证用户的电子邮件。问题是,当我运行该应用程序时,它显示了以下消息:java.lang.NoClassDefFoundError:我使用的是java 17,javafx 17,netbeans ide 12.0。
这是我写的代码:
package chat;
import java.util.Properties;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Chat extends Application{
private Parent raiz;
private Scene scene;
private Stage stg1;
private Image imagenx;
@Override
public void start(Stage stage) throws Exception {
raiz = FXMLLoader.load(getClass().getResource("Chat.fxml"));
scene = new Scene(raiz);
scene.setUserAgentStylesheet(getClass().getResource("chat.css").toExternalForm());
stg1 = stage;
stg1.setScene(scene);
imagenx = new Image(getClass().getResource("icono.jpg").toString());
stg1.getIcons().add(imagenx);
stg1.setTitle("Video chat");
stg1.setWidth(800);
stg1.setHeight(600);
stg1.show();
}
private void enviarMail(String destinatario){
String remitente = "mi remitente";
String asunto = "enviar correo de confirmacion";
String cuerpo = "mensaje a enviar";
String clv = "1234";
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.user", remitente);
props.put("mail.smtp.clave", clv);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "587");
Autenticador aut = new Autenticador(remitente,clv);
sesion = Session.getInstance(props,aut);
mm = new MimeMessage(sesion);
try{
mm.setFrom(new InternetAddress(remitente));
mm.addRecipients(Message.RecipientType.TO, destinatario);
mm.setSubject(asunto);
mm.setText(cuerpo);
try(Transport transport = sesion.getTransport("smtp")) {
transport.connect("smtp.gmail.com", remitente, clv);
try{
transport.sendMessage(mm, mm.getAllRecipients());
}
catch(MessagingException ex){
System.out.println(ex);
}
}
catch(Exception ex){
System.out.println(ex);
}
}
catch(MessagingException ex){
System.out.println(ex);
}
}
public static void main(String[] args) {
launch(args);
enviarMail("example1@gmail.com");
}
}
class Autenticador extends Authenticator{
private String smtp_usuario = "";
private String smtp_pass = "";
Autenticador() {}
Autenticador(String user , String pass) {
this.smtp_usuario = user;
this.smtp_pass = pass;
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.smtp_usuario,
this.smtp_pass);
}
}
错误是在以下代码行中生成的:mm =新Mime消息(会话);
将库从javax.mail更改为jakarta.mail,将netbeans更新为版本12. 6,我安装了版本java 17,添加jar jakarta.activation,但当我在netbeans中运行项目时,会生成以下错误:java.lang.NoClassDefFoundError:我发现我应该添加以下依赖项:
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.0.1</version>
</dependency>
查询,我应该在哪里添加pom.xml文件和依赖项,以及如何使用jakarta.activation jar?我是自学成才的,所以这个问题可能非常明显。除了JavaMail,还有其他库可以用来发送电子邮件吗?
1条答案
按热度按时间soat7uwm1#
Jakarta Mail is the modern packaging for Java mail.
See the Jakarta Mail readme :
Jakarta Mail requires Jakarta Activation 2.0.0 or newer.
Jakarta Activation website and download .
If you use a build tool (highly recommended), such as Maven or Gradle, it will likely already add a transitive dependency on Jakarta activation to your build when you add the Jakarta mail dependency. If it doesn’t, then add the dependency yourself.
Use recent versions of all software, JDK/JavaFX 17.0.2+, JakartaMail 2.0.1+. See:
Also, see jakarta mail compatability notes
In this release the name of that module is changed to "jakarta.mail".
So use
jakarta.mail
in yourmodule-info.Java
when requiring the moduleOR
Place
jakarta.mail
in your--add-module
switch, if adding modules via command line switches instead ofmodule-info.java
.Example App
The following app compiles and runs but will fail when trying to send mail due to incorrect authentication, however, I think it demonstrates how to fix the issues with class resolution asked in the question.
Things it does:
requires jakarta.mail
in the module-info.