java - 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");
}
任何关于如何解决这些问题的建议都非常受欢迎,尤其是第二个。非常感谢您的宝贵时间!
解决方案
推荐阅读
- javascript - 格式化来自 Slack 的消息
- gmail-api - How can we mark a message as answered using the GMail API?
- r - Shiny:反应式表达式的条件格式(涉及的模块)
- javascript - 调用函数以更改对象在数组中的位置 (JavaScript)
- python-3.x - 根据坐标值减少多个数组
- r - 如何在 R 中并行运行 for 循环
- java - 覆盖 fillInStackTrace() 的正确方法是什么?
- google-bigquery - 将 JSON 数据流式传输到 BigQuery 时重复
- azure - Auth0 使用什么 IP 地址范围连接到自定义数据库?
- c++ - GNU 链接器脚本 - 如何自动将代码分发到多个不连续的部分