spring实现邮件发送
<!-- mail start --><bean id="mailSender" value="smtp.163.com" /> <!-- 配置邮件服务器--><property name="username" value="????@163.com"></property><!-- 配置发送邮件的邮箱--> <property name="password" value="????"></property><!-- 配置邮箱密码--><property name="defaultEncoding" value="utf-8"></property><!-- 配置默认编码--><property name="javaMailProperties"><props> <prop key="mail.smtp.auth">true</prop> <!--如果邮箱需要密码这个地方必须为true --></props></property></bean><!-- this is a template message that we can pre-load with default state --><bean id="templateMessage" value="????@163.com" /><property name="subject" value="谢谢" /></bean><!-- mail end -->
?配置邮件发送的类和接口
public class EmailManagerImpl implements EmailManager {@Autowired@Qualifier("mailSender") private MailSender mailSender;@Autowired@Qualifier("templateMessage") private SimpleMailMessage templateMessage; public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } public void setTemplateMessage(SimpleMailMessage templateMessage) { this.templateMessage = templateMessage; } public void sendMail() { SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage); msg.setTo("????@qq.com"); msg.setText( "Dear" + ", 谢谢你注册我们的网站! "); try{ this.mailSender.send(msg); } catch(MailException ex) { // simply log it and go on... System.err.println(ex.getMessage()); } }
?之后在需要的地方调用sendMail()方法就好了。
?