JavaFX layout not updating and email sending optimization problem

  • Context: Java 
  • Thread starter Thread starter archaic
  • Start date Start date
  • Tags Tags
    Email Optimization
Click For Summary
SUMMARY

The discussion focuses on optimizing a JavaFX application for bulk email sending, highlighting two main issues: performance, with a delay of approximately 15 seconds for every 5 emails sent, and the JavaFX scene not updating correctly during the email sending process. The provided code demonstrates the use of JavaMail API for sending emails and the JavaFX framework for the user interface. The primary solution identified is addressing the concurrency problem, as indicated by the reference to the Oracle concurrency tutorial.

PREREQUISITES
  • JavaFX 8 for building the user interface
  • JavaMail API for sending emails
  • Understanding of concurrency in Java applications
  • Basic knowledge of FXML for JavaFX layout management
NEXT STEPS
  • Implement JavaFX Task or Service for handling email sending in a background thread
  • Explore JavaMail API documentation for advanced email features
  • Review JavaFX concurrency tutorial for best practices
  • Investigate performance optimization techniques for bulk email sending
USEFUL FOR

Java developers, particularly those working with JavaFX and email applications, as well as anyone looking to improve the performance and responsiveness of their Java applications.

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

Similar threads

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