开发者

Spring - config apache commons email

开发者 https://www.devze.com 2022-12-14 13:35 出处:网络
I want to use HtmlEmail in apache commons-email in a spring app, so i use the config xml as following:

I want to use HtmlEmail in apache commons-email in a spring app, so i use the config xml as following:

<bean id="commonsEmail" class="org.apache.commons.mail.HtmlEmail">
    <property name="hostName" value="smtp.example.com" />
    <property name="TLS" value="true"/>
    <property name="smtpPort" value="587"/>
</bean>

But i can't initialize it because of the smtpPort property:

Invalid property 'smtpPort' of bean class [org.apache.commons.mail.HtmlEmail]: Bean property 'smtpPort' is no开发者_开发百科t writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

Please tell me what i've done wrong ? Thank you.


This is happening because the smtpPort property is ambiguous - the getSmtpPort method returns a String, but the setSmtpPort method takes an int. Spring gets cold feet at this point, and throws the exception saying that the bean property is invalid.

I think both HtmlEmail and Spring are at fault here - HtmlEmail for bad API design, Spring for being unnecessarily pedantic.

The solution I'd recommend is one of:

  1. Create your own subclass of HtmlEmail, defining a new setter method, with a new name, which delegates to setSmtpPort. This is quick and easy, but is rather poor design in itself.

  2. Write an implementation of Spring's FactoryBean interface, which gets the job of instantiating and configuring an HtmlEmail instance. This is more work than (1), but is a cleaner design.

  3. Ditch Commons Email completely, and use Spring's own Email abstraction layer. This would be my recommended option.

0

精彩评论

暂无评论...
验证码 换一张
取 消