I think the issue is that JavaMailSenderImpl either let's you use a pre-defined session or define properties for your own session, but it does not let you add or override properties to a pre-defined session. Because JavaMailSenderImpl does not declare it's methods final, you can extend JavaMailSenderImpl to allow users to override or add properties to an existing java mail session.
Code:
public class MyJavaMailSenderImpl extends JavaMailSenderImpl implements InitializingBean {
private Properties javaMailProperties;
/* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
if (javaMailProperties!=null && getSession()!=null) {
Properties properties = getSession().getProperties();
properties.putAll(javaMailProperties);
setSession(Session.getInstance(properties));
return;
}
if (javaMailProperties!=null) {
super.setJavaMailProperties(javaMailProperties);
}
}
public void setJavaMailProperties(Properties javaMailProperties) {
this.javaMailProperties = javaMailProperties;
}
}
The code above checks to see if both javaMailProperties and session have been set. If they are set, we add the javaMailProperties to the existing session properties. Because this seems like a limitation on the original JavaMailSenderImpl, in the long term, I think it makes sense to apply these changes to JavaMailSenderImpl. Thoughts?