The recipients specified in the message headers always take precedence over recipients pre-configured in the endpoint URI. The idea is that if you provide any recipients in the message headers, that is what you get. The recipients pre-configured in the endpoint URI are treated as a fallback.
In the sample code below, the email message is sent to
email@example.com, because it takes precedence over the pre-configured
settings in the endpoint URI are also ignored and those recipients will not receive any
mail. The choice between headers and pre-configured settings is all or nothing: the mail
component either takes the recipients exclusively from
the headers or exclusively from the pre-configured settings. It is not possible to mix and
match headers and pre-configured settings.
Map<String, Object> headers = new HashMap<String, Object>(); headers.put("to", "firstname.lastname@example.org"); template.sendBodyAndHeaders( "smtp://admin@email@example.com", "Hello World", headers);