Skip to content

JavaMailSenderImpl should re-use protocol from mail session when creating transport instance [SPR-8501] #13147

@spring-projects-issues

Description

@spring-projects-issues

Robin Sander opened SPR-8501 and commented

Currently it's not possible to configure the protocol in a mail session because JavaMailSenderImpl always uses it's own 'protocol' property to create the transport instance.
For example, if I define a mail resource in a container using protocol 'smtps':

<Resource name="mail/Session" auth="Container" type="javax.mail.Session"
     mail.transport.protocol="smtps" mail.smtps.host="..." mail.smtps.port="465" mail.smtps.auth="true"
     ...
     mail.smtps.starttls.enable="true" mail.smtps.socketFactory.class="javax.net.ssl.SSLSocketFactory" />

JavaMailSenderImpl creates the transport instance like this:

protected Transport getTransport(Session session) throws NoSuchProviderException {
  return session.getTransport(getProtocol());
}

so I would have to set the protocol' property to 'smtps' as well.
Now while it may be convenient to be able to choose a provider by protocol it should also be possible to re-use the protocol which is defined in the mail session. My proposal is to use null or an empty protocol property in this case:

protected Transport getTransport(Session session) throws NoSuchProviderException {
  String p = getProtocol();
  return (p == null || p.length() ==0) ? session.getTransport() : session.getTransport(p);
}

Affects: 3.0.5

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions