Java JavaFX layout not updating and email sending optimization problem

Click For Summary
The discussion centers on developing a Java application for bulk email sending, highlighting two primary issues: performance and JavaFX scene updates. The application currently processes emails at a rate of approximately 15 seconds for every five emails sent. The more pressing concern is that the JavaFX interface does not update correctly during the sending process. The code provided shows an implementation where the sending label should reflect the current email being processed, while also updating the counts for successfully sent and failed emails.The problem is identified as a concurrency issue, which affects the responsiveness of the GUI during the email-sending operation. The user seeks advice on resolving these issues, particularly the GUI update problem, and references a tutorial on JavaFX concurrency for potential solutions.
archaic
Messages
688
Reaction score
214
I am writing a java application that would let me bulk send emails.

The first problem I have is that of performance; approximately 15 seconds per 5 emails.

The second problem, which is the more important, is that my JavaFX is not updating the scene. My code below shows that the way I intended this to work is that it should display the email that is currently being treated, and that the "Envoyés" and "Échecs" labels get updated each time.

handleSend() is the onClick() like method for the button basically.

Java:
package test;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;

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

The GUI is as such (the sending label is hidden when the program launches, of course):
lt3o7.png

Any advice on how to solve these problems is most welcome, especially the second. Thank you very much for your time!
 
Technology news on Phys.org
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
4
Views
3K
Replies
12
Views
7K