发送电子邮件错误

时间:2018-06-22 13:40:28

标签: java javafx smtp javamail

我在JavaFX应用程序中发送电子邮件时遇到一些问题。当我只是用一个简单的程序(没有GUI)测试发送电子邮件时,一切正常,我能够发送电子邮件。但是,当我将其集成到我的应用程序中时,就会遇到此异常。 引起原因:java.lang.ClassNotFoundException:javax.mail.MessagingException

我添加了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());
        }

    }

}

运行时抛出异常:

Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 73 more

当我在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();

enter image description here

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

这是我更改屏幕(阶段)的方式:

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);
}

0 个答案:

没有答案
相关问题