JavaFX布局未更新和电子邮件发送优化问题

JavaFX布局未更新和电子邮件发送优化问题,java,email,javafx,Java,Email,Javafx,我正在编写一个java应用程序,它可以让我批量发送电子邮件 我遇到的第一个问题是绩效问题;每5封电子邮件大约15秒 第二个更重要的问题是,我的JavaFX没有更新场景。我下面的代码表明,我打算这样做的方式是,它应该显示当前正在处理的电子邮件,“特使”和“埃切克”标签每次都会更新 handleSend()基本上是按钮的onClick()类似方法 public class AppController { @FXML private TextArea emails; @FX

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

我遇到的第一个问题是绩效问题;每5封电子邮件大约15秒

第二个更重要的问题是,我的JavaFX没有更新场景。我下面的代码表明,我打算这样做的方式是,它应该显示当前正在处理的电子邮件,“特使”和“埃切克”标签每次都会更新

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");
    }
公共类AppController{
@FXML
私人短信区电子邮件;
@FXML
私人标签发送;
@FXML
自有品牌失败;
@FXML
私标发送;
@FXML
无效handleSend(ActionEvent事件){
字符串[]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”、“我的主机”);
props.put(“mail.smtp.user”,“我的电子邮件”);
props.put(“mail.smtp.password”、“我的密码”);
props.put(“mail.smtp.port”,“587”);
props.put(“mail.smtp.auth”,“true”);
Session Session=Session.getDefaultInstance(props);
运输;;
试一试{
transport=session.getTransport(“smtp”);
传输连接(“smtp”、“我的电子邮件”、“我的密码”);
int count=0;//查看发送了多少
长时间=System.currentTimeMillis();//用于运行时
对于(int i=0;i
GUI就是这样(当然,当程序启动时,发送标签是隐藏的):


非常欢迎您就如何解决这些问题提供任何建议,尤其是第二个。非常感谢您抽出时间!

我的问题在维护之后被打断了…-。请看一下教程JavaFX+并发性:有关更多帮助,请发布hey@c0der!问题是,我不知道到底是什么原因导致程序无法运行正如所料。不过,我遵循了@Puce的建议,并且能够修复它。我的问题在维护之后被打断了…-。请看一下教程JavaFX+并发性:要获得更多帮助,请发布hey@c0der!问题是,我不知道是什么导致程序无法按预期运行。我遵循了@Puce的建议,尽管如此,他还是能够修复它。