Java 发送电子邮件错误

Java 发送电子邮件错误,java,javafx,smtp,jakarta-mail,Java,Javafx,Smtp,Jakarta Mail,我在JavaFX应用程序中发送电子邮件时遇到一些问题。当我刚刚用一个简单的程序(没有GUI)测试发送电子邮件时,一切都很顺利,我能够发送电子邮件。然而,当我集成到我的应用程序中时,我遇到了这个例外。 原因:java.lang.ClassNotFoundException:javax.mail.MessaginException 我添加了jar文件(activation.jar、javax.mail.jar、smtp-1.6.0.jar) 以下是源代码: import com.jfoenix.co

我在JavaFX应用程序中发送电子邮件时遇到一些问题。当我刚刚用一个简单的程序(没有GUI)测试发送电子邮件时,一切都很顺利,我能够发送电子邮件。然而,当我集成到我的应用程序中时,我遇到了这个例外。 原因:java.lang.ClassNotFoundException:javax.mail.MessaginException

我添加了jar文件(activation.jar、javax.mail.jar、smtp-1.6.0.jar)

以下是源代码:

import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextArea;
import com.jfoenix.controls.JFXTextField;
import dao.classes.KlientiHibernateDao;
import dao.interfaces.KlientiDao;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javax.imageio.ImageIO;
import models.Klienti;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import utils.HibernateUtil;
import utils.MyAlert;
import java.util.*;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
// import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage;

public class EmailViewController implements Initializable {

    @FXML
    private JFXTextField emailTF;
    @FXML
    private JFXPasswordField fjalekalimiTF;
    @FXML
    private JFXComboBox<String> marresiteCB;
    @FXML
    private JFXTextField subjektiTF;
    @FXML
    private JFXTextArea mesazhiTA;
    @FXML
    private ImageView imageView;

    private Alert alert;

    SceneChanger sc = new SceneChanger();

    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session;
    Transaction tx;
    KlientiDao kdao;

    public void butoniSend(ActionEvent event) {
        session = factory.getCurrentSession();
        tx = session.beginTransaction();
        kdao = new KlientiHibernateDao();
        kdao.setSession(session);

        List<Klienti> klientat = null;
        String[] marresit = null;

        try {
            String host = "smtp.gmail.com";
            String user = emailTF.getText();
            String pass = fjalekalimiTF.getText();

            if (marresiteCB.getValue().startsWith("T")) {
                klientat = kdao.findAll();
                marresit = new String[klientat.size()];
            }

            for (int i = 0; i < marresit.length; i++) {
                marresit[i] = klientat.get(i).getEmail();
            }


            String from = emailTF.getText();
            String subject = subjektiTF.getText();
            String messageText = mesazhiTA.getText();
            boolean sessionDebug = false;

            Properties props = System.getProperties();

            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.required", "true");

            java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props, null);
            mailSession.setDebug(sessionDebug);
            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] adresat = new InternetAddress[marresit.length];

            for (int i = 0; i < adresat.length; i++) {
                adresat[i] = new InternetAddress(marresit[i]);
            }

            msg.setRecipients(Message.RecipientType.TO, adresat);
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            msg.setText(messageText);

            Transport transport = mailSession.getTransport("smtp");
            transport.connect(host, user, pass);
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
            alert = MyAlert.returnConfirmationAlert("Alert", "Email shkoi me sukses!");
        } catch (MessagingException e) {
            alert = MyAlert.returnErrorAlert("Alert", e.getMessage());
            System.out.println(e.getMessage()); 
        }

        tx.commit();
        session.close();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        emailTF.setText("auto.x.prishtina@gmail.com");
        emailTF.setEditable(false);
        marresiteCB.getItems().addAll("Të gjithë klientët", "Klientët VIP");
        subjektiTF.setText("Përshëndetje i/e nderuar!");
        mesazhiTA.setText("Përshëndetje i/e nderuar!\n\n" + "Mesazhi i email-it" + "\n\nMe respekt,\nAuto X");

        try {
            File imgFile = new File("./src/imazhet/icons/email_icon.png");
            BufferedImage bufferedImage = ImageIO.read(imgFile);
            Image image = SwingFXUtils.toFXImage(bufferedImage, null);
            imageView.setImage(image);

        } catch (IOException e) {
            System.err.println(e.getMessage());
        }

    }

}
当我在main方法中执行此操作时,它会起作用:

// Parent root = FXMLLoader.load(getClass().getResource("LoginView.fxml"));
Parent root = FXMLLoader.load(getClass().getResource("EmailView.fxml"));

        Scene scene = new Scene(root);
        pS.setTitle("Login");
        pS.setScene(scene);
        pS.show();

但我不想从那个场景运行我的应用程序,我想从登录屏幕运行

以下是我如何更改屏幕(阶段)的方法:

我认为这是一个错误:

public void butoniEmailPromocional(ActionEvent event) throws IOException {
    sc.changeScenes(event, "EmailView.fxml", "Email promocional", true);
}

您是否以相同的方式运行程序?您可以检查,将代码移动到应用程序类的
main
方法是否会导致相同的错误(假设
应用程序
类是您程序的入口点)。这可能是类路径的问题…@fabian添加
class.forName就足够了(“javax.mail.MessaginException”);
作为main()中的第一条语句方法。当jar丢失时,则会出现相同的异常。我认为问题在于更改阶段,但我并不真正理解原因。但在主方法中,我会这样做:父根=FXMLLoader.load(getClass().getResource(“EmailView.fxml”));场景场景=新场景(根);pS.setTitle(“登录”);pS.setScene(场景);pS.show();很好。@Fabian-我编辑了这个问题,请重新查看。您是如何构建代码的?您使用什么进行依赖关系管理的?
public class SceneChanger {
    /*
        This method will accept the the title of the new scene, the .fxml file name
        for the view and the ActionEvent that triggered the change
    */
    public void changeScenes(ActionEvent event, String viewName, String title, boolean resizable) throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource(viewName));
        Parent parent = loader.load();

        Scene scene = new Scene(parent);

        // Get the scene from the event that was passed in
        Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
        stage.setTitle(title);

        stage.setScene(scene);
        stage.setResizable(resizable);
        stage.show();

    }
}
public void butoniEmailPromocional(ActionEvent event) throws IOException {
    sc.changeScenes(event, "EmailView.fxml", "Email promocional", true);
}