This commit is contained in:
Tour
2025-12-08 08:14:42 +01:00
parent 3cc0d40fa3
commit 35851c124c
3 changed files with 54 additions and 6 deletions

View File

@@ -52,32 +52,46 @@ public record NotificationService(Config cfg) {
var props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
// Connection timeouts (10 seconds each)
props.put("mail.smtp.connectiontimeout", "10000");
props.put("mail.smtp.timeout", "10000");
props.put("mail.smtp.writetimeout", "10000");
var session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(cfg.smtpUsername(), cfg.smtpPassword());
}
});
// Enable debug mode for troubleshooting (can be disabled in production)
// session.setDebug(true);
var m = new MimeMessage(session);
m.setFrom(new InternetAddress(cfg.smtpUsername()));
m.setRecipients(Message.RecipientType.TO, InternetAddress.parse(cfg.toEmail()));
m.setSubject("[Troostwijk] " + title);
m.setText(msg);
m.setSentDate(new Date());
if (prio > 0) {
m.setHeader("X-Priority", "1");
m.setHeader("Importance", "High");
}
Transport.send(m);
log.info("Email notification: {}", title);
log.info("Email notification sent: {}", title);
} catch (javax.mail.AuthenticationFailedException e) {
log.warn("Email authentication failed - check Gmail App Password: {}", e.getMessage());
} catch (javax.mail.MessagingException e) {
log.warn("Email connection failed (network/firewall issue?): {}", e.getMessage());
} catch (Exception e) {
log.warn("Email failed: {}", e.getMessage());
}