Spring - 配置 apache commons 电子邮件

发布于 2024-08-15 12:15:02 字数 568 浏览 1 评论 0原文

我想在 Spring 应用程序中的 apache commons-email 中使用 HtmlEmail,因此我使用配置 xml,如下所示:

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

但由于 smtpPort 属性,我无法初始化它:

Invalid property 'smtpPort' of bean class [org.apache.xml] commons.mail.HtmlEmail]:Bean 属性“smtpPort”不可写或具有无效的 setter 方法。 setter 的参数类型与 getter 的返回类型是否匹配?

请告诉我我做错了什么?谢谢。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-08-22 12:15:02

发生这种情况是因为 smtpPort 属性不明确 - getSmtpPort 方法返回 String,但 setSmtpPort 方法采用一个int。 Spring 在这一点上临阵退缩,并抛出异常,指出 bean 属性无效。

我认为 HtmlEmail 和 Spring 都有问题 - HtmlEmail 的 API 设计很糟糕,Spring 则显得不必要的迂腐。

我推荐的解决方案是以下之一:

  1. 创建您自己的 HtmlEmail 子类,使用新名称定义一个新的 setter 方法,该方法委托给 setSmtpPort。这既快速又简单,但其本身的设计相当糟糕。

  2. 编写 Spring 的 FactoryBean 接口的实现,它负责实例化和配置 HtmlEmail 实例。这比(1)需要更多工作,但设计更简洁。

  3. 完全放弃 Commons Email,并使用 Spring自己的电子邮件抽象层。这将是我推荐的选项。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文