user1563721
user1563721

Reputation: 1571

Spring MVC JavaMailSenderImpl parameters from properties for javaMailProperties

I have a spring configuration of JavaMailSenderImpl included in my mvc.properties file like this:

email.smtp.host=smtp.example.com
email.smtp.port=25
email.smtp.username=example
email.smtp.password=example
email.smtp.auth=false
email.smtp.starttls.enable=false

And my XML configuration of the mail sending bean in servlet is following:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${email.smtp.host}"/>
        <property name="port" value="${email.smtp.port}"/>
        <property name="username" value="${email.smtp.username}"/>
        <property name="password" value="${email.smtp.password}"/>
        <property name="protocol" value="smtp"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
            </props>
        </property>
    </bean>

I was trying to find information about how to include parameters from properties file also for javaMailProperties, but I am stuck.

I would like to achieve something like that, but this is not working:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
            <property name="host" value="${email.smtp.host}"/>
            <property name="port" value="${email.smtp.port}"/>
            <property name="username" value="${email.smtp.username}"/>
            <property name="password" value="${email.smtp.password}"/>
            <property name="protocol" value="smtp"/>
            <property name="javaMailProperties">
                <props>
                    <prop key="mail.smtp.auth" value="${email.smtp.auth}"></prop>
                    <prop key="mail.smtp.starttls.enable" value="${email.smtp.starttls.enable}"></prop>
                </props>
            </property>
        </bean>

How to include such properties also into javaMailProperties?

Upvotes: 1

Views: 828

Answers (1)

user2722345
user2722345

Reputation: 11

The way I am doing this by referencing settings stored in a configuration file looks something like this:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${email.smtp.host}"/>
    <property name="port" value="${email.smtp.port}"/>
    <property name="username" value="${email.smtp.username}"/>
    <property name="password" value="${email.smtp.password}"/>
    <property name="protocol" value="smtp"/>
    <property name="javaMailProperties" ref="propertyConfigurer"/>
</bean>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:foo.properties</value>
            <value>classpath:mail.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

Upvotes: 1

Related Questions