You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2026/04/10 09:31:38 UTC
(camel) branch main updated: CAMEL-23308: camel-mail - Using a custom JavaMailSender should still be default configured (#22540)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 3c93fcef5149 CAMEL-23308: camel-mail - Using a custom JavaMailSender should still be default configured (#22540)
3c93fcef5149 is described below
commit 3c93fcef5149ef7facf9162e7196e25144fc7630
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Apr 10 11:31:30 2026 +0200
CAMEL-23308: camel-mail - Using a custom JavaMailSender should still be default configured (#22540)
---
.../camel/component/mail/MailConfiguration.java | 97 ++++++++++++----------
.../apache/camel/component/mail/MailEndpoint.java | 2 +
.../apache/camel/component/mail/MailProducer.java | 1 +
.../component/mail/MailCustomMailSenderTest.java | 11 ++-
.../camel/component/mail/MailEndpointTlsTest.java | 12 ++-
.../ROOT/pages/camel-4x-upgrade-guide-4_19.adoc | 5 ++
6 files changed, 77 insertions(+), 51 deletions(-)
diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
index 1036ea4c9009..b54110601788 100644
--- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
+++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
@@ -205,71 +205,76 @@ public class MailConfiguration implements Cloneable {
}
protected JavaMailSender createJavaMailSender(CamelContext context) {
- JavaMailSender answer = new DefaultJavaMailSender();
+ return new DefaultJavaMailSender();
+ }
- if (javaMailProperties != null) {
- answer.setJavaMailProperties(javaMailProperties);
- } else {
- // set default properties if none provided
- answer.setJavaMailProperties(createJavaMailProperties(context));
- // add additional properties if provided
- if (additionalJavaMailProperties != null) {
- answer.getJavaMailProperties().putAll(additionalJavaMailProperties);
+ protected void configureJavaMailSender(CamelContext context, JavaMailSender answer) {
+ if (answer.getJavaMailProperties() == null || answer.getJavaMailProperties().isEmpty()) {
+ if (javaMailProperties != null) {
+ answer.setJavaMailProperties(javaMailProperties);
+ } else {
+ // set default properties if none provided
+ answer.setJavaMailProperties(createJavaMailProperties(context));
+ // add additional properties if provided
+ if (additionalJavaMailProperties != null) {
+ answer.getJavaMailProperties().putAll(additionalJavaMailProperties);
+ }
}
}
- if (host != null) {
+ if (answer.getHost() == null && host != null) {
answer.setHost(host);
}
- if (port >= 0) {
+ if (answer.getPort() <= 0 && port >= 0) {
answer.setPort(port);
}
- if (username != null) {
+ if (answer.getUsername() == null && username != null) {
answer.setUsername(username);
}
- if (password != null) {
+ if (answer.getPassword() == null && password != null) {
answer.setPassword(password);
}
- if (authenticator != null) {
+ if (answer.getAuthenticator() == null && authenticator != null) {
answer.setAuthenticator(authenticator);
}
- if (protocol != null) {
+ if (answer.getProtocol() == null && protocol != null) {
answer.setProtocol(protocol);
}
- if (session != null) {
- answer.setSession(session);
- String hostPropertyValue = session.getProperty("mail.smtp.host");
- if (hostPropertyValue == null || hostPropertyValue.isEmpty()) {
- hostPropertyValue = session.getProperty("mail.smtps.host");
- }
- if (hostPropertyValue != null && !hostPropertyValue.isEmpty()) {
- answer.setHost(hostPropertyValue);
- }
- String portPropertyValue = session.getProperty("mail.smtp.port");
- if (portPropertyValue == null || portPropertyValue.isEmpty()) {
- portPropertyValue = session.getProperty("mail.smtps.port");
- }
- if (portPropertyValue != null && !portPropertyValue.isEmpty()) {
- answer.setPort(Integer.parseInt(portPropertyValue));
- }
- } else {
- ClassLoader tccl = Thread.currentThread().getContextClassLoader();
- try {
- if (applicationClassLoader != null) {
- Thread.currentThread().setContextClassLoader(applicationClassLoader);
+ if (answer.getSession() == null) {
+ if (session != null) {
+ answer.setSession(session);
+ String hostPropertyValue = session.getProperty("mail.smtp.host");
+ if (hostPropertyValue == null || hostPropertyValue.isEmpty()) {
+ hostPropertyValue = session.getProperty("mail.smtps.host");
+ }
+ if (hostPropertyValue != null && !hostPropertyValue.isEmpty()) {
+ answer.setHost(hostPropertyValue);
+ }
+ String portPropertyValue = session.getProperty("mail.smtp.port");
+ if (portPropertyValue == null || portPropertyValue.isEmpty()) {
+ portPropertyValue = session.getProperty("mail.smtps.port");
+ }
+ if (portPropertyValue != null && !portPropertyValue.isEmpty()) {
+ answer.setPort(Integer.parseInt(portPropertyValue));
+ }
+ } else {
+ ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+ try {
+ if (applicationClassLoader != null) {
+ Thread.currentThread().setContextClassLoader(applicationClassLoader);
+ }
+ // use our authenticator that does not live user interaction but returns the already configured username and password
+ Session sessionInstance = Session.getInstance(answer.getJavaMailProperties(),
+ authenticator == null
+ ? new DefaultAuthenticator(answer.getUsername(), answer.getPassword()) : authenticator);
+ // sets the debug mode of the underlying mail framework
+ sessionInstance.setDebug(debugMode);
+ answer.setSession(sessionInstance);
+ } finally {
+ Thread.currentThread().setContextClassLoader(tccl);
}
- // use our authenticator that does not live user interaction but returns the already configured username and password
- Session sessionInstance = Session.getInstance(answer.getJavaMailProperties(),
- authenticator == null ? new DefaultAuthenticator(getUsername(), getPassword()) : authenticator);
- // sets the debug mode of the underlying mail framework
- sessionInstance.setDebug(debugMode);
- answer.setSession(sessionInstance);
- } finally {
- Thread.currentThread().setContextClassLoader(tccl);
}
}
-
- return answer;
}
private Properties createJavaMailProperties(CamelContext context) {
diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java
index c178fe7350c7..fddb518f54a8 100644
--- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java
+++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java
@@ -122,6 +122,7 @@ public class MailEndpoint extends ScheduledPollEndpoint implements HeaderFilterS
// use default mail sender
sender = configuration.createJavaMailSender(getCamelContext());
}
+ configuration.configureJavaMailSender(getCamelContext(), sender);
return createProducer(sender);
}
@@ -142,6 +143,7 @@ public class MailEndpoint extends ScheduledPollEndpoint implements HeaderFilterS
// must use java mail sender impl as we need to get hold of a mail session
JavaMailSender sender = configuration.createJavaMailSender(getCamelContext());
+ configuration.configureJavaMailSender(getCamelContext(), sender);
return createConsumer(processor, sender);
}
diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
index d1ab2f2a5532..5b84f28ff1e1 100644
--- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
+++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
@@ -103,6 +103,7 @@ public class MailProducer extends DefaultAsyncProducer {
LOG.debug("Creating new JavaMailSender to include additional {} java mail properties", additional.size());
JavaMailSender customSender
= getEndpoint().getConfiguration().createJavaMailSender(getEndpoint().getCamelContext());
+ getEndpoint().getConfiguration().configureJavaMailSender(getEndpoint().getCamelContext(), customSender);
final String scheme = prefix;
additional.forEach((k, v) -> {
if (v != null) {
diff --git a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailCustomMailSenderTest.java b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailCustomMailSenderTest.java
index f922b77db776..a59ea8db2ce3 100644
--- a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailCustomMailSenderTest.java
+++ b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailCustomMailSenderTest.java
@@ -27,6 +27,7 @@ import org.apache.camel.component.mail.Mailbox.Protocol;
import org.apache.camel.test.junit6.CamelTestSupport;
import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class MailCustomMailSenderTest extends CamelTestSupport {
@@ -42,10 +43,15 @@ public class MailCustomMailSenderTest extends CamelTestSupport {
sendBody(claus.uriPrefix(Protocol.smtp) + "&javaMailSender=#mySender", "Hello World");
assertTrue(sent, "Should have used custom mail sender");
+
+ // should also be configured
+ assertEquals(Protocol.smtp.name(), sender.getProtocol());
}
private static class MySender implements JavaMailSender {
+ private String protocol;
+
@Override
public void send(MimeMessage mimeMessage) {
sent = true;
@@ -53,7 +59,7 @@ public class MailCustomMailSenderTest extends CamelTestSupport {
@Override
public Properties getJavaMailProperties() {
- return null;
+ return new Properties();
}
@Override
@@ -102,11 +108,12 @@ public class MailCustomMailSenderTest extends CamelTestSupport {
@Override
public void setProtocol(String protocol) {
+ this.protocol = protocol;
}
@Override
public String getProtocol() {
- return null;
+ return protocol;
}
@Override
diff --git a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailEndpointTlsTest.java b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailEndpointTlsTest.java
index 54b28d971e78..75ea3cc46273 100644
--- a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailEndpointTlsTest.java
+++ b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailEndpointTlsTest.java
@@ -58,7 +58,9 @@ public class MailEndpointTlsTest extends CamelTestSupport {
assertTrue(cfg.isStartTlsEnabled());
- Properties javaMailProperties = cfg.createJavaMailSender(context).getJavaMailProperties();
+ var sender = cfg.createJavaMailSender(context);
+ cfg.configureJavaMailSender(context, sender);
+ Properties javaMailProperties = sender.getJavaMailProperties();
assertNull(javaMailProperties.get("mail." + protocol + ".ssl.socketFactory"));
assertNull(javaMailProperties.get("mail." + protocol + ".ssl.socketFactory.port"));
}
@@ -74,7 +76,9 @@ public class MailEndpointTlsTest extends CamelTestSupport {
cfg.setPassword("secret");
cfg.setSslContextParameters(MailTestHelper.createSslContextParameters());
- Properties javaMailProperties = cfg.createJavaMailSender(context).getJavaMailProperties();
+ var sender = cfg.createJavaMailSender(context);
+ cfg.configureJavaMailSender(context, sender);
+ Properties javaMailProperties = sender.getJavaMailProperties();
assertFalse(cfg.isStartTlsEnabled());
@@ -108,7 +112,9 @@ public class MailEndpointTlsTest extends CamelTestSupport {
assertTrue(cfg.isStartTlsEnabled());
- Properties javaMailProperties = cfg.createJavaMailSender(context).getJavaMailProperties();
+ var sender = cfg.createJavaMailSender(context);
+ cfg.configureJavaMailSender(context, sender);
+ Properties javaMailProperties = sender.getJavaMailProperties();
assertNotNull(javaMailProperties.get("mail." + protocol + ".ssl.socketFactory"));
assertNotNull(javaMailProperties.get("mail." + protocol + ".ssl.socketFactory.port"));
}
diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_19.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_19.adoc
index de3ff64803d4..a1cc5627cccd 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_19.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_19.adoc
@@ -509,6 +509,11 @@ documentation for migration guidance.
Debezium has been upgraded from 3.4.x to 3.5.0.
+=== camel-mail
+
+When using a custom `MailSender` implementation then Camel will now configure this instances as well, with the various
+settings for hostname, protocol, port, etc.
+
=== camel-ftp
The `camel-ftp` component has been refactored to extract shared FTP/SFTP code into a new `camel-ftp-common` module.