春天:从Gmail发送电子邮件

编程入门 行业动态 更新时间:2024-10-28 02:34:58
本文介绍了春天:从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" /> <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.mkyongmon.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" /> <property name="to" value="to@no-spam" /> <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 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发送电子邮件

本文发布于:2023-10-07 10:23:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469128.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送电子邮件   春天   Gmail

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!