Spring 通过 Google 发送电子邮件 ()
下面的代码运行良好
但是,我的问题是如何以编程方式设置“javaMailProperties”? 因为我想从代码中设置 ssl/tsl 。我无法访问这些属性,我不知道为什么,感谢您的解决方案和解释。
SimpleMailMessage message=(SimpleMailMessage)SpringUtil.getContext().
getBean("templateMessage");
JavaMailSenderImpl mailSender = (JavaMailSenderImpl)SpringUtil.getContext()
.getBean("mailSender");
mailSender.send(message);
--applicationcontext.xml--
<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="your gmail address"/>
<property name="password" value="your 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="templateMessage" class="org.springframework.mail.SimpleMailMessage" >
<property name="from" value="[email protected]"/>
<property name="to" value="[email protected]"/>
<property name="subject" value="subject"/>
<property name="text" value="hello"/>
</bean>
(我不想使用javax.mail方法,有人问)
The code below works well
But, my question is how can set the 'javaMailProperties' programaticaly ?
Because I would like to set ssl/tsl from the code. I could not access these properties, I dont know why, thanks for the solution and explanation.
SimpleMailMessage message=(SimpleMailMessage)SpringUtil.getContext().
getBean("templateMessage");
JavaMailSenderImpl mailSender = (JavaMailSenderImpl)SpringUtil.getContext()
.getBean("mailSender");
mailSender.send(message);
--applicationcontext.xml--
<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="your gmail address"/>
<property name="password" value="your 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="templateMessage" class="org.springframework.mail.SimpleMailMessage" >
<property name="from" value="[email protected]"/>
<property name="to" value="[email protected]"/>
<property name="subject" value="subject"/>
<property name="text" value="hello"/>
</bean>
(I dont want to use javax.mail approach, it was asked)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用途:
与您想要以编程方式设置的其他属性相同。
编辑:
我检查了
JavaMailSenderImpl
类的源代码:如您所见,getJavaMailProperties是一个公共方法,应该可供您使用。我的Spring框架版本是3.0.5。
Use:
Same for other properties that you want to set programatically.
EDIT:
I checked the source code for the
JavaMailSenderImpl
class:As you can see, the getJavaMailProperties is a public method and should be available to you. My Spring framework version is 3.0.5.
快速谷歌未验证。
在 mailSender.getJavaMailProperties.setProperty("mail.smtp.auth",true) 上调用 setProperty(String key, String value)
http://download.oracle.com/javase/6/docs/api/java/util/Properties.html
A quick google not verified.
Call setProperty(String key, String value) on the mailSender.getJavaMailProperties.setProperty("mail.smtp.auth",true)
http://download.oracle.com/javase/6/docs/api/java/util/Properties.html