来自javaMailProperties属性的Spring MVC JavaMailSenderImpl参数

时间:2018-06-19 17:11:28

标签: spring spring-mvc properties smtp javamail

我的JavaMailSenderImpl文件中包含mvc.properties的spring配置,如下所示:

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

我在servlet中对邮件发送bean的XML配置如下:

<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>

我正试图从属性文件中为javaMailProperties查找有关如何包括参数的信息,但是我被困住了。

我想实现类似的目的,但这不起作用:

<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>

如何将此类属性也包含到javaMailProperties中?

1 个答案:

答案 0 :(得分:1)

我通过引用存储在配置文件中的设置来执行此操作的方式如下:

<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>