春天:从Gmail发送电子邮件 [英] Spring: Send email from Gmail
本文介绍了春天:从Gmail发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在关注发送的此链接电子邮件(Gmail smtp)
我的问题是,为什么我应该在bean中对发件人和接收器进行硬编码?
I am following This link for sending email (Gmail smtp) My problem is that why should I hardcode sender and receiver in the bean?
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="username" />
<property name="password" value="password" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
<bean id="mailMail" class="com.mkyong.common.MailMail">
<property name="mailSender" ref="mailSender" />
<property name="simpleMailMessage" ref="customeMailMessage" />
</bean>
<bean id="customeMailMessage"
class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="from@no-spam.com" />
<property name="to" value="to@no-spam.com" />
<property name="subject" value="Testing Subject" />
<property name="text">
<value>
<![CDATA[
Dear %s,
Mail Content : %s
]]>
</value>
</property>
</bean>
推荐答案
您可以避免对电子邮件属性进行硬编码,电子邮件属性在外部属性文件中,例如 email.properties
。如果您在配置文件中启用上下文
命名空间,Spring将加载属性文件,并允许文件中的属性通过表达式语言使用。
You can avoid hard coding the email properties by placing the email properties in an external properties file, say email.properties
. If you enable the context
namespace within your configuration file Spring will load the properties file and allow properties within the file to be used via the expression language.
Email.properties
email.host=smtp.gmail.com
email.port=587
email.username=username
email.password=password
配置文件
<!-- Spring Loads the Properties File, which can be used for resolution of EL Expressions -->
<context:property-placeholder location="classpath:META-INF/db/db.properties"/>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${email.host}" />
<property name="port" value="${email.port}" />
<property name="username" value="${email.username}" />
<property name="password" value="${email.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
这篇关于春天:从Gmail发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文