Andrea Cosentino created CAMEL-27929:
----------------------------------------

             Summary: camel-mail - keep header-derived JavaMail properties 
scoped to each message
                 Key: CAMEL-27929
                 URL: https://issues.apache.org/jira/browse/CAMEL-27929
             Project: Camel
          Issue Type: Improvement
          Components: camel-mail
            Reporter: Andrea Cosentino
            Assignee: Andrea Cosentino
             Fix For: 4.25.0


h3. Background

The camel-mail producer has an opt-in option 
{{useJavaMailSessionPropertiesFromHeaders}} (default {{false}}). When it is 
enabled, a message carrying {{mail.smtp.*}} / {{mail.smtps.*}} headers is sent 
through a dedicated per-message {{JavaMailSender}} so that those header-derived 
JavaMail properties apply to that one message only.

h3. Current behaviour

When the endpoint is configured with a {{javaMailProperties}} bean, 
{{MailConfiguration.configureJavaMailSender()}} assigns that same 
{{Properties}} instance to every sender it configures, by reference and without 
copying (around lines 355-367). For the per-message sender, 
{{MailProducer.getSender()}} then writes the header-derived values into that 
instance through {{DefaultJavaMailSender.addAdditionalJavaMailProperty()}}, 
which calls {{getJavaMailProperties().setProperty(...)}}.

That {{Properties}} instance is the long-lived endpoint object, shared with the 
endpoint's default sender. So the values intended for a single message are 
retained on the endpoint after that message is sent:

* later messages that do not carry the headers are sent through the default 
sender, whose {{Session}} reads the same backing {{Properties}}, so they pick 
up the retained values;
* concurrent sends mutate and read the one shared object at the same time, so 
values set for one in-flight send can appear on another.

The values that were meant to apply to one message therefore persist for every 
later message of the endpoint.

When {{javaMailProperties}} is not set, 
{{MailConfiguration.createJavaMailProperties()}} builds a fresh {{Properties}} 
for each sender (around lines 424-459), so each sender owns its own instance 
and the carry-over does not happen. Setting only 
{{additionalJavaMailProperties}} likewise does not trigger it.

Separately, when {{session}} is configured on the endpoint, the per-message 
header properties are still written to {{javaMailProperties}}, but the sender 
uses the endpoint {{Session}}'s own properties, so the header values have no 
effect and are silently ignored.

h3. Proposed change

Keep the per-message, header-derived properties scoped to the message:

* In {{MailProducer.getSender()}}, before calling 
{{configureJavaMailSender()}}, pre-seed the per-message custom sender with a 
defensive copy of the endpoint {{javaMailProperties}} when it is set ({{new 
Properties()}} then {{putAll(base)}}). The subsequent 
{{addAdditionalJavaMailProperty()}} writes then land on the copy, and the 
shared endpoint {{Properties}} and the default sender are left untouched. The 
copy must be in place before the sender's {{Session}} is built inside 
{{configureJavaMailSender()}}, otherwise the per-message values would stop 
taking effect.
* Alternatively, fix it at the source: in {{configureJavaMailSender()}}, assign 
a clone instead of the reference ({{(Properties) javaMailProperties.clone()}}), 
so every sender owns its own instance - consistent with the 
no-{{javaMailProperties}} branch, which already builds a fresh object per 
sender.

Also clarify, in the component documentation, that the header-derived 
properties are not applied when {{session}} is set on the endpoint; optionally 
log a one-time {{WARN}} from {{getSender()}} in that case, so the option does 
not appear to work when it cannot.

h3. Classes involved

* {{MailProducer.getSender()}} - 
components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
 (primary change)
* {{MailConfiguration.configureJavaMailSender()}} and 
{{createJavaMailProperties()}} - 
components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
 (the by-reference assignment and the fresh-per-call branch)
* {{DefaultJavaMailSender.getJavaMailProperties()}} / 
{{addAdditionalJavaMailProperty()}} - 
components/camel-mail/src/main/java/org/apache/camel/component/mail/DefaultJavaMailSender.java
 (the lazy getter and the per-message write)

h3. Tests

* New JUnit test {{MailProducerPerMessageSessionPropertiesTest}} using a 
recording/stub {{JavaMailSender}} (as in {{MailCustomMailSenderTest}}), with no 
network: with the endpoint {{javaMailProperties}} set and 
{{useJavaMailSessionPropertiesFromHeaders=true}}, send message A with a 
{{mail.smtp.*}} header, then message B without it, and assert that B's sender 
does not observe A's value and that the endpoint's shared 
{{javaMailProperties}} is unchanged after A.
* A unit assertion that the per-message sender's {{Properties}} is a distinct 
instance from the endpoint's when {{javaMailProperties}} is configured.
* camel-mail tests are predominantly JUnit, so keep JUnit assertions and 
{{MockEndpoint}} timed assertions, and no {{Thread.sleep}}.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to