我正在使用JavaMail编写一个小型Java应用程序,向用户发送自动电子邮件.他们可以选择(现在)两个端口:25和587.可以通过GUI上的单选按钮选择端口.
我添加了一个测试按钮,允许用户测试电子邮件设置(包括端口).但是,由于某种原因,一旦用户尝试发送测试电子邮件,端口就无法更改.Javamail将始终使用原始测试电子邮件的端口.
示例:用户尝试在端口25上发送电子邮件,JavaMail表示无法在端口25上连接(例如,SMTP主机使用另一个端口).用户单击端口587,并尝试发送新电子邮件.JavaMail抛出一个错误,说它无法再次在端口25上连接.
我有点难过为什么.每次发送新的测试电子邮件时,都会创建一个全新的SendMailUsingAuthentication对象.在该类中,属性始终重置为正确的端口.每当我调试时,据我所知,所有变量都是正确的,并与正确的端口相关联.我错过了运输内部的东西吗?
在前端GUI中:
private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {
int port = port25RadioButton.isSelected() ? PORT_25 : PORT_587;
notifier = new SendMailUsingAuthentication(hostNameTextField.getText(),
userTextField.getText(), getPassword(), emailTextField.getText().split(","),port);
Thread wait = new Thread(new Runnable() {
public void run() {
try {
changeStatusText("Sending test email...");
notifier.postTestMail();
changeStatusText("Test email sent.");
} catch (AddressException ex) {
changeStatusText("Error. Invalid email address name.");
} catch (MessagingException ex) {
changeStatusText("SMTP host connection refused.");
System.err.println(ex.getMessage());
} catch (Exception ex) {
System.err.println(ex);
}
}
});
wait.start();
}
Run Code Online (Sandbox Code Playgroud)
在电子邮件发件人类中:
public void postTestMail() …Run Code Online (Sandbox Code Playgroud) 你得到一个大小为n的数组,包含任意的布尔值.
返回随机TRUE值索引的最快方法是什么.
该算法应随机返回包含TRUE的任何一个索引.