Set the mail.from to avoid looking up the local hostname

Closes #38353

Signed-off-by: Alexander Schwartz <alexander.schwartz@gmx.net>
This commit is contained in:
Alexander Schwartz
2025-03-22 22:28:11 +01:00
committed by Marek Posolda
parent 3d1714a25b
commit 83e99f7617
2 changed files with 15 additions and 1 deletions

View File

@@ -91,7 +91,7 @@ public class DefaultEmailSenderProvider implements EmailSenderProvider {
}
}
private Properties buildEmailProperties(Map<String, String> config) {
private Properties buildEmailProperties(Map<String, String> config) throws EmailException {
Properties props = new Properties();
if (config.containsKey("host")) {
@@ -136,6 +136,17 @@ public class DefaultEmailSenderProvider implements EmailSenderProvider {
if (isNotBlank(envelopeFrom)) {
props.setProperty("mail.smtp.from", envelopeFrom);
}
String from = config.get("from");
if (from == null) {
throw new EmailException("No sender address configured in the realm settings for emails");
}
// Specify 'mail.from' as InternetAddress.getLocalAddress() would otherwise do a InetAddress.getCanonicalHostName
// and add this as a mail header. This would both be slow, and would reveal internal IP addresses that we don't want.
// https://jakarta.ee/specifications/mail/2.0/jakarta-mail-spec-2.0#a823
props.setProperty("mail.from", from);
return props;
}

View File

@@ -2,6 +2,8 @@ package org.keycloak.test.examples;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.events.email.EmailEventListenerProviderFactory;
@@ -48,6 +50,7 @@ public class EmailTest {
mail.waitForIncomingEmail(1);
MimeMessage lastReceivedMessage = mail.getLastReceivedMessage();
Assertions.assertEquals("Login error", lastReceivedMessage.getSubject());
MatcherAssert.assertThat(lastReceivedMessage.getMessageID(), Matchers.endsWith("@keycloak.org>"));
}
public static class EmailSenderRealmConfig implements RealmConfig {