Sto scrivendo una piccola app Java utilizzando JavaMail che invia all'utente una e-mail automatica. Possono scegliere tra (per ora) due porte: 25 e 587. La porta può essere selezionata tramite un pulsante di opzione sulla GUI.Come modificare la porta JavaMail
Ho aggiunto un pulsante di test per consentire all'utente di testare le impostazioni di posta elettronica (inclusa la porta). Tuttavia, per qualche motivo, una volta che l'utente tenta di inviare un'e-mail di prova, la porta non può essere modificata. Javamail utilizzerà sempre la porta dell'e-mail di prova originale.
Esempio: l'utente tenta di inviare un'e-mail sulla porta 25 e JavaMail dice che non può connettersi sulla porta 25 (ad esempio, l'host SMTP utilizza un'altra porta). L'utente fa clic sulla porta 587 e tenta di inviare una nuova email. JavaMail genera un errore che dice che non può connettersi nuovamente sulla porta 25, di nuovo.
Sono un po 'perplesso sul perché. Ogni volta che viene inviata una nuova email di prova viene creato un oggetto SendMailUsingAuthentication completamente nuovo. All'interno di quella classe le proprietà vengono sempre reimpostate sulla porta corretta. Ogni volta che eseguo il debug, per quanto posso vedere, tutte le variabili sono corrette e associate alla porta corretta. C'è qualcosa che sta accadendo all'interno di Transport che mi manca?
Alla fine GUI front:
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();
}
Nella classe e-mail del mittente:
public void postTestMail() throws MessagingException, AddressException{
String[] testReciever = new String[1];
testReciever[0] = emailList[0];
postMail(testReciever, "Test email.", "Your email settings are successfully set up.", emailFromAddress);
}
private void postMail(String recipients[], String subject,
String message, String from) throws MessagingException, AddressException {
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.host", smtpHostName);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", true);
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(false);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}