fix-tests-cleanup

This commit is contained in:
Tour
2025-12-08 13:40:00 +01:00
parent c46c0fe21e
commit 255f1e81dd

View File

@@ -4,9 +4,9 @@ import javax.mail.Authenticator;
import javax.mail.Message; import javax.mail.Message;
import javax.mail.PasswordAuthentication; import javax.mail.PasswordAuthentication;
import javax.mail.Session; import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import com.sun.mail.smtp.SMTPTransport;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.awt.SystemTray; import java.awt.SystemTray;
@@ -79,6 +79,8 @@ public record NotificationService(Config cfg) {
} }
private void sendEmail(String title, String msg, int prio) { private void sendEmail(String title, String msg, int prio) {
SMTPTransport transport = null;
boolean sent = false;
try { try {
var props = new Properties(); var props = new Properties();
props.put("mail.smtp.auth", "true"); props.put("mail.smtp.auth", "true");
@@ -88,6 +90,8 @@ public record NotificationService(Config cfg) {
props.put("mail.smtp.port", "587"); props.put("mail.smtp.port", "587");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com"); props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
props.put("mail.smtp.ssl.protocols", "TLSv1.2"); props.put("mail.smtp.ssl.protocols", "TLSv1.2");
// Avoid waiting for QUIT reply (prevents "Exception reading response" after successful send)
props.put("mail.smtp.quitwait", "false");
// Connection timeouts (configurable; short during tests, longer otherwise) // Connection timeouts (configurable; short during tests, longer otherwise)
int smtpTimeoutMs = Integer.getInteger("auctiora.smtp.timeout.ms", isUnderTest() ? 200 : 10000); int smtpTimeoutMs = Integer.getInteger("auctiora.smtp.timeout.ms", isUnderTest() ? 200 : 10000);
@@ -97,7 +101,6 @@ public record NotificationService(Config cfg) {
props.put("mail.smtp.writetimeout", t); props.put("mail.smtp.writetimeout", t);
var session = Session.getInstance(props, new Authenticator() { var session = Session.getInstance(props, new Authenticator() {
@Override @Override
protected PasswordAuthentication getPasswordAuthentication() { protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(cfg.smtpUsername(), cfg.smtpPassword()); return new PasswordAuthentication(cfg.smtpUsername(), cfg.smtpPassword());
@@ -116,14 +119,34 @@ public record NotificationService(Config cfg) {
m.setHeader("Importance", "High"); m.setHeader("Importance", "High");
} }
Transport.send(m); transport = (SMTPTransport) session.getTransport("smtp");
transport.connect("smtp.gmail.com", cfg.smtpUsername(), cfg.smtpPassword());
transport.sendMessage(m, m.getAllRecipients());
sent = true;
log.info("Email notification sent: {}", title); log.info("Email notification sent: {}", title);
String resp = transport.getLastServerResponse();
if (resp != null) {
log.debug("SMTP response: {}", resp);
}
} catch (javax.mail.AuthenticationFailedException e) { } catch (javax.mail.AuthenticationFailedException e) {
log.warn("Email authentication failed - check Gmail App Password: {}", e.getMessage()); log.warn("Email authentication failed - check Gmail App Password: {}", e.getMessage());
} catch (javax.mail.MessagingException e) { } catch (javax.mail.MessagingException e) {
if (sent) {
// Message accepted by server; subsequent connection close caused exception
log.info("Email sent (connection closed early): {}", e.getMessage());
} else {
log.warn("Email connection failed (network/firewall issue?): {}", e.getMessage()); log.warn("Email connection failed (network/firewall issue?): {}", e.getMessage());
}
} catch (Exception e) { } catch (Exception e) {
log.warn("Email failed: {}", e.getMessage()); log.warn("Email failed: {}", e.getMessage());
} finally {
if (transport != null) {
try {
transport.close();
} catch (Exception ignored) {
}
}
} }
} }