首页 > 解决方案 > JavaFX 布局未更新和电子邮件发送优化问题

问题描述

我正在编写一个可以让我批量发送电子邮件的 java 应用程序。

我遇到的第一个问题是性能问题。每 5 封电子邮件大约需要 15 秒。

第二个更重要的问题是我的 JavaFX 没有更新场景。我下面的代码显示,我打算让它工作的方式是它应该显示当前正在处理的电子邮件,并且“Envoyés”和“Échecs”标签每次都会更新。

handleSend()基本上是按钮的onClick()类似方法。

public class AppController {

    @FXML
    private TextArea emails;

    @FXML
    private Label sent;

    @FXML
    private Label failed;

    @FXML
    private Label sending;

    @FXML
    void handleSend(ActionEvent event) {
        String[] emailsList = emails.getText().split("\n", -1);
        //emails.setDisable(true);
        
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "my host");
        props.put("mail.smtp.user", "my email");
        props.put("mail.smtp.password", "my password");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);

        Transport transport;
        try {
            transport = session.getTransport("smtp");
            transport.connect("smtp", "my email", "my password");
            
            int count = 0; // to see how many get sent
            long time = System.currentTimeMillis(); // for the runtime
            for (int i = 0; i < emailsList.length; ++i) {
                sending.setText("En cours: " + emailsList[i]); // the email that is being treated
                MimeMessage message = new MimeMessage(session);

                try {
                    message.setFrom(new InternetAddress("my email", "what i want others to see as my name"));
                } catch (UnsupportedEncodingException | MessagingException e) {
                    e.printStackTrace();
                }

                // Transport transport;
                try {
                    message.setSubject("subject");
                    message.setContent("big html paragraph", "text/html; charset=utf-8");
                    message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailsList[i]));

                    transport.sendMessage(message, message.getAllRecipients());
                    System.out.println("Mail " + (i + 1) + " enoyé avec succès à " + emailsList[i]); // for the console
                    sent.setText("Envoyés: " + (++count)); // number of sent mails updated each iteration
                    failed.setText("Échecs: " + (i + 1 - count)); // the same for the fails
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
            transport.close();

            time = System.currentTimeMillis() - time;
            sending.setText(String.format("%d mail(s) envoyés en %.2fs.", count, ((double) time) / 1000));
            // total count of emails that got sent + runtime
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        
        //emails.setDisable(false);
    }

    @FXML
    void initialize() {
        sending.setText("");
        sent.setText("Envoyés: 0");
        failed.setText("Échecs: 0");
    }

GUI 是这样的(当然,程序启动时发送标签是隐藏的):
在此处输入图像描述

任何关于如何解决这些问题的建议都非常受欢迎,尤其是第二个。非常感谢您的宝贵时间!

标签: javaemailjavafx

解决方案


推荐阅读